简体   繁体   中英

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);
registerForContextMenu(cmenu);

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. I'm not sure, but you can check by setting a breakpoint on the

super.onCreateContextMenu(menu, v, menuInfo);

line, and checking the debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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