gpt4 book ai didi

java - 如何将不同的菜单项添加到多个上下文菜单?

转载 作者:太空狗 更新时间:2023-10-29 16:09:52 25 4
gpt4 key购买 nike

我有两个按钮,每个按钮都有一个上下文菜单,但我不确定如何更改第二个菜单中的菜单项。我的代码只显示我的第一个按钮的项目。

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;
}

最佳答案

在onCreateContextMenu中比较哪个按钮作为View传入:

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
}
}

关于java - 如何将不同的菜单项添加到多个上下文菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5294590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com