我如何不同的菜单项添加到多个上下文菜单? [英] How can I add different menu items to multiple context menus?
本文介绍了我如何不同的菜单项添加到多个上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个按钮,每一个上下文菜单,但我不能确定如何更改第二个菜单的菜单项。我的code只显示我的第一个按钮,我的项目。
按钮Button1的=(按钮)findViewById(R.id.Button1);
registerForContextMenu(Button1的);按钮将Button2 =(按钮)findViewById(R.id.Button3);
registerForContextMenu(Button2的);
}
@覆盖
公共无效onCreateContextMenu(文本菜单菜单视图V,ContextMenuInfo menuInfo){
super.onCreateContextMenu(菜单,V,menuInfo);
menu.setHeaderTitle(选择通知);
menu.add(0,v.getId(),0,代数);
menu.add(0,v.getId(),0,转向节列车);
menu.add(0,v.getId(),0,在我的元素);
menu.add(0,v.getId(),0,让我们这方);
menu.add(0,v.getId(),0,Shmowzow);
menu.add(0,v.getId(),0,什么壶);
menu.add(0,v.getId(),0,字去你妈);
}@覆盖
公共布尔onContextItemSelected(菜单项项){
如果(item.getTitle()==代数){功能1(item.getItemId());}
否则,如果(item.getTitle()==转向节列车){函数2(item.getItemId());}
否则,如果(item.getTitle()==在我的元素){function3(item.getItemId());}
否则,如果(item.getTitle()==让我们这个党){function4(item.getItemId());}
否则如果(item.getTitle()==Shmowzow){function5(item.getItemId());}
否则,如果(item.getTitle()==什么壶){function6(item.getItemId());}
否则,如果(item.getTitle()==字去你妈){因数7(item.getItemId());}
其他{返回false;}
返回true;
}
解决方案
比较哪个按钮被传入作为onCreateContextMenu查看:
公共无效onCreateContextMenu(文本菜单菜单视图V,ContextMenuInfo menuInfo){
super.onCreateContextMenu(菜单,V,menuInfo);
如果(V == Button1的){
menu.setHeaderTitle(选择通知);
menu.add(0,v.getId(),0,代数);
menu.add(0,v.getId(),0,转向节列车);
menu.add(0,v.getId(),0,在我的元素);
menu.add(0,v.getId(),0,让我们这方);
menu.add(0,v.getId(),0,Shmowzow);
menu.add(0,v.getId(),0,什么壶);
menu.add(0,v.getId(),0,字去你妈);
}否则如果(V ==将Button2){
//为Button2的东西
}
}
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
}
}
这篇关于我如何不同的菜单项添加到多个上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文