简体   繁体   English

如何判断哪个按钮访问了上下文菜单

[英]How can I tell which button has accessed the context menu

In my soundboard app I have created a context menu using this code. 在我的音板应用程序中,我使用此代码创建了一个上下文菜单。

public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Choose an option");
        menu.add(0, v.getId(), 0, "Save as ringtone");
        menu.add(0, v.getId(), 0, "Save as Notification");
        menu.add(0, v.getId(), 0, "Save as Alarm");
        menu.add(0, v.getId(), 0, "Exit Menu");
}


   @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    //    Global.currentsound = info.id;
        if(item.getTitle()=="Save as ringtone"){function1(item.getItemId());}
        else if(item.getTitle()=="Save as Notification"){function2(item.getItemId());}
        else if(item.getTitle()=="Save as Alarm"){function3(item.getItemId());}
        else {return false;}
    return true;
    }

and called on the buttons like this 并调用这样的按钮

Button cmenu = (Button)findViewById(R.id.s1sound1); 按钮cmenu =(按钮)findViewById(R.id.s1sound1);
registerForContextMenu(cmenu); registerForContextMenu(c菜单);

Now I want to pass the information for each button to the function in the code to set the sound according to which button was pressed. 现在我想将每个按钮的信息传递给代码中的函数,以根据按下的按钮设置声音。 How would I do this without creating a separate context menu for each button which would be madness. 如果不为每个按钮创建一个疯狂的单独的上下文菜单,我该如何做到这一点。

Thanks 谢谢

I think it is stored in the "View v", Button extends View, so you have to cast it. 我认为它存储在“View v”中,Button扩展了View,所以你必须将其强制转换。 I'm not sure, but you can check by setting a breakpoint on the 我不确定,但您可以通过设置断点来检查

super.onCreateContextMenu(menu, v, menuInfo);

line, and checking the debugger. 行,并检查调试器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM