[英]Get menu item to (un)check it
I want to get a menu item to use the .setChecked()
. 我想获得一个菜单项来使用.setChecked()
。 But this should be in another function than the onOptionsItemSelected(MenuItem item)
. 但这应该是在onOptionsItemSelected(MenuItem item)
之外的另一个函数中。 So I tried it with findViewById()
but then I mentioned that this won't work^^ I googled but I can only find examples where they user it in the onOptionsItemSelected function and can use the item
object. 所以我尝试使用findViewById()
但后来我提到这将无法正常工作^^我用Google搜索但我只能找到他们在onOptionsItemSelected函数中使用它的示例,并且可以使用item
对象。 Is there any possibility to get the menu item like findViewById
? 是否有可能获得像findViewById
这样的菜单项?
I want to get a menu item to use the .setChecked(). 我想获得一个菜单项来使用.setChecked()。 But this should be in another function than the onOptionsItemSelected(MenuItem item) 但这应该是在onOptionsItemSelected(MenuItem项)之外的另一个函数中
My first idea is to create "copy" of your Menu
and then you have access to its child items. 我的第一个想法是创建Menu
“副本”,然后您可以访问其子项。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menuCopy = menu;
new MenuInflater(<context>).inflate(layout, this.menuCopy);
return super.onCreateOptionsMenu(this.menuCopy);
}
And then you can use two methods: 然后你可以使用两种方法:
menu.getItem()
it returns menu item at the given index menu.getItem()
它返回给定索引处的菜单项 menu.findItem()
it returns menu item with a particular identifier menu.findItem()
它返回带有特定标识符的菜单项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.