Android的动作栏可检查的菜单项无效/显示正常吗? [英] Android action bar checkable menu item does not work/show properly?
问题描述
所以我试图让我的菜单项,即显示操作栏上的表现就像一个可检查的菜单选项。而杉杉的部分工作,这意味着它是可检查的,当我preSS它,并且在$ C c中的setChecked(真)$设置它的工作原理。但是,什么行不通的视觉部分。有一个在如何一个菜单项上看起来在选中和未选中状态的操作栏没有改变?我尝试使用invalidateOptionsMenu(),但是,这并不做这项工作,不仅如此,在我的code那条线,我不能走出选中状态呢?!? 什么情况是,无效OptionsMenu()接缝来取消选中状态,我结束了'循环',或者该菜单项我坚持要去的code中的未检查的部分在那里它被检查了每preSS和无效它会选中我猜...
下面是我的XML文件菜单中的code:
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@ + ID / lenslist_menu_add
机器人:showAsAction =总是
机器人:标题=@字符串/添加/>
<项目机器人:ID =@ + ID / lenslist_menu_delete
机器人:showAsAction =总是
机器人:可检查=真
机器人:标题=@字符串/删除/>
< /菜单>
这里是java code:
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
// TODO自动生成方法存根
开关(item.getItemId()){
案例R.id.lenslist_menu_add:
返回true;
案例R.id.lenslist_menu_delete:
如果(item.isChecked()==真){
item.setChecked(假);
deleteMode = FALSE;
lensAdapter.setDeleteMode(假);
} 其他 {
item.setChecked(真正的);
deleteMode = TRUE;
lensAdapter.setDeleteMode(真正的);
}
lensAdapter.notifyDataSetChanged();
返回true;
}
返回super.onOptionsItemSelected(项目);
}
谢谢!
可检查的项目只出现在子菜单或上下文菜单。
您使用的是他们的主菜单中的项目,因此它不会工作。
来源:下载API的演示,并打开文件ApiDemos / RES /菜单/ checkable.xml,你会看到它作为第13行的注释,我不知道他们为什么不提这个的开发者文档
与评论参考: <一href="http://alvinalexander.com/java/jwarehouse/android-examples/platforms/android-2/samples/ApiDemos/res/menu/checkable.xml.shtml">http://alvinalexander.com/java/jwarehouse/android-examples/platforms/android-2/samples/ApiDemos/res/menu/checkable.xml.shtml
so I am trying to get my menu item, that is show on the action bar to behave like a checkable menu option. The firs part works, meaning it is checkable and when I press it, and set in code the setChecked(true) it works. But what does not work is the visual part. There is no change in how a menu item looks on the action bar in checked and unchecked states? I tried using invalidateOptionsMenu() but that does not do the job, and not only that, with that line in my code I can't get out of the checked state?!? What happens is that invalidate OptionsMenu() seams to unset the checked state and I end up 'looping', or on every press of that menu item I keep going to the unchecked part of the code where it gets checked and with invalidate it gets unchecked I guess...
Here is the code from my XML file for menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/lenslist_menu_add"
android:showAsAction="always"
android:title="@string/add"/>
<item android:id="@+id/lenslist_menu_delete"
android:showAsAction="always"
android:checkable="true"
android:title="@string/delete"/>
</menu>
And here is the java code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.lenslist_menu_add:
return true;
case R.id.lenslist_menu_delete:
if (item.isChecked() == true) {
item.setChecked(false);
deleteMode = false;
lensAdapter.setDeleteMode(false);
} else {
item.setChecked(true);
deleteMode = true;
lensAdapter.setDeleteMode(true);
}
lensAdapter.notifyDataSetChanged();
return true;
}
return super.onOptionsItemSelected(item);
}
Thanks!
Checkable items appear only in submenus or context menus.
You are using them as main menu items, hence it will not work.
SOURCE: Download the API DEMOS, and open the file ApiDemos/res/menu/checkable.xml, you'll see it as a comment on line 13. I don't know why they don't mention this in the Developer Documentation
reference with comment.: http://alvinalexander.com/java/jwarehouse/android-examples/platforms/android-2/samples/ApiDemos/res/menu/checkable.xml.shtml
这篇关于Android的动作栏可检查的菜单项无效/显示正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!