get view id from oncontextitemselected

前端 未结 5 1007
旧巷少年郎
旧巷少年郎 2020-12-11 20:11

I\'ve several buttons registered for context menu

how do I know which button was clicked for the menu to appear?

below is the pseudocode that i\'ll be using.

5条回答
  •  时光取名叫无心
    2020-12-11 20:38

    Ok, thanks alot for the help from the others which clear my doubts on the getItemId since it returns the ID that I assigned to the menu item. In my case, I wanted to know which button was clicked before the contextmenu was created.

    To do this, I simply create a long variable to store the button that was clicked. The ID of the button can be obtained as in the following:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Send As..");
        menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
        menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
        btnId = v.getId(); //this is where I get the id of my clicked button
    }
    

    and later on I'll only need to refer to this btnId to do whatever I want.

提交回复
热议问题