简体   繁体   中英

How can I add different menu items to multiple context menus?

I have two button each with a Context Menu, but I'm unsure how to change the menu items in the second menu. My code only shows my items for my first button.

Button Button1 = (Button) findViewById(R.id.Button1);
registerForContextMenu(Button1); 

Button Button2 = (Button) findViewById(R.id.Button3);
registerForContextMenu(Button2); 
}


@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Notification");
menu.add(0, v.getId(), 0, "Algebraic");
menu.add(0, v.getId(), 0, "Knuckle Train");
menu.add(0, v.getId(), 0, "In My Element ");
menu.add(0, v.getId(), 0, "Let's Get This Party");
menu.add(0, v.getId(), 0, "Shmowzow");
menu.add(0, v.getId(), 0, "What the Jug");
menu.add(0, v.getId(), 0, "Word to Your Mother");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Algebraic"){function1(item.getItemId());}
else if(item.getTitle()=="Knuckle Train"){function2(item.getItemId());}  
else if(item.getTitle()=="In My Element"){function3(item.getItemId());} 
else if(item.getTitle()=="Let's Get This Party"){function4(item.getItemId());}  
else if(item.getTitle()=="Shmowzow"){function5(item.getItemId());}  
else if(item.getTitle()=="What the Jug"){function6(item.getItemId());}  
else if(item.getTitle()=="Word to Your Mother"){function7(item.getItemId());}  
else {return false;}
return true;
}

Compare which button was passed in as the View in onCreateContextMenu:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v == Button1) {
        menu.setHeaderTitle("Select Notification");
        menu.add(0, v.getId(), 0, "Algebraic");
        menu.add(0, v.getId(), 0, "Knuckle Train");
        menu.add(0, v.getId(), 0, "In My Element ");
        menu.add(0, v.getId(), 0, "Let's Get This Party");
        menu.add(0, v.getId(), 0, "Shmowzow");
        menu.add(0, v.getId(), 0, "What the Jug");
        menu.add(0, v.getId(), 0, "Word to Your Mother");
    } else if (v == Button2) {
        // stuff for Button2
    }
}

Adding ContextMenu item for different widget in Android is very easy. Let me explain how to do this.

For example there are two widgets in your XML file containing layout for your activity.

  1. txtnum1
  2. txtnum2

You have set the listener for both of them using below given code

this.registerForContextMenu(txtnum1); this.registerForContextMenu(txtnum2);

Then you override the following method

@Override 
public void onCreateContextMenu(ContextMenu mymenu, View v, ContextMenuInfo menuInfo)

Now let us consider that if user long press on txtnum1 , you want to show two item square and qube as context menu item and if user long press on txtnum2 you want to show xxx and yyy as context menu item.

It should be done using below given code

@Override
public void onCreateContextMenu(ContextMenu mymenu, View v, ContextMenuInfo menuInfo)
{
    Toast.makeText(this,String.valueOf(v.getId()),1000).show();
    if(v.equals(txtnum1)==true)
    {
        mymenu.add(0,1,0,"Square");
        mymenu.add(0,2,0,"Qube");
    }
    else if(v.equals(txtnum2)==true)
    {
        mymenu.add(0,1,0,"xxx");
        mymenu.add(0,2,0,"yyy");
    }
    super.onCreateContextMenu(mymenu, v, menuInfo);
}

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