如何启用运行/禁用上下文菜单项 [英] How to enable/disable context menu items on runtime
问题描述
我想启用/禁用项目根据一定的条件下...
通过选项菜单中,你可以做:
.setGroupEnabled(0,假);
但我不知道怎么跟上下文菜单做到这一点。
我怎么能这样做?
感谢
编辑:这些项目是在膨胀的菜单
@覆盖
公共无效onCreateContextMenu(文本菜单菜单视图V,ContextMenuInfo menuInfo){ super.onCreateContextMenu(菜单,V,menuInfo); MenuInflater吹气= getMenuInflater(); inflater.inflate(R.menu.options,菜单);
}
您可以修改/从 onCreateContextMenu
方法中在运行时创建一个上下文菜单。
@覆盖
公共无效onCreateContextMenu(文本菜单菜单视图V,ContextMenuInfo menuInfo){
super.onCreateContextMenu(菜单,V,menuInfo); AdapterContextMenuInfo信息=(AdapterContextMenuInfo)menuInfo;
为MyObject OBJ =(为MyObject)myListView.getItemAtPosition(info.position); menu.setHeaderTitle(选择选项);
menu.add(0,v.getId(),0,删除+ obj.name);
}
下面是一些示例code,我从<一花href=\"http://stackoverflow.com/questions/3631967/how-to-create-a-dynamic-context-menu-in-android\">this帖子。通过重写此方法,你可以修改菜单的创建。只是这里内添加一些条件语句来控制你的不同scenerios。
I want to enable/disable items depending certain conditions...
With options menu you can do:
.setGroupEnabled(0, false);
But i don't know how to do this with context menu
How can I do that?
Thanks
EDIT: These items are in inflated menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
}
You can modify/create a context menu at runtime from within the onCreateContextMenu
method.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
MyObject obj = (MyObject) myListView.getItemAtPosition(info.position);
menu.setHeaderTitle("Selection Options");
menu.add(0, v.getId(), 0, "Remove " + obj.name);
}
Here is some sample code I took from this post. By overriding this method you can modify the menu as its created. Just add some conditionals within here to control your different scenerios.
这篇关于如何启用运行/禁用上下文菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!