Android 操作栏可检查菜单项无法正常工作/显示?

编程入门 行业动态 更新时间:2024-10-25 17:25:07
本文介绍了Android 操作栏可检查菜单项无法正常工作/显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我试图获取我的菜单项,即显示在操作栏上的行为就像一个可检查的菜单选项.第一部分有效,这意味着它是可检查的,当我按下它时,并在代码中设置 setChecked(true) 它有效.但不起作用的是视觉部分.在选中和未选中状态下,菜单项在操作栏上的外观没有变化?我尝试使用 invalidateOptionsMenu() 但这不起作用,不仅如此,在我的代码中使用该行我无法退出选中状态?!?发生的情况是,使 OptionsMenu() 无效的接缝取消选中的状态,我最终循环",或者在每次按下该菜单项时,我都会继续转到代码的未选中部分,在那里它被选中,而 invalidate 它被取消选中我猜...

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

这是我的 XML 文件中的菜单代码:

Here is the code from my XML file for menu:

<menu xmlns:android="schemas.android/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>

这是java代码:

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

谢谢!

推荐答案

可勾选的项目只出现在子菜单或上下文菜单中.

Checkable items appear only in submenus or context menus.

您将它们用作主菜单项,因此它不起作用.

You are using them as main menu items, hence it will not work.

来源: 下载 API DEMOS,打开文件 ApiDemos/res/menu/checkable.xml,你会在第 13 行看到它作为注释.我不知道他们为什么不要在开发者文档中提及这一点

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

参考评论.:/alvinalexander/java/jwarehouse/android-examples/platforms/android-2/samples/ApiDemos/res/menu/checkable.xml.shtml

更多推荐

Android 操作栏可检查菜单项无法正常工作/显示?

本文发布于:2023-11-08 07:59:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568804.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   菜单项   操作   工作   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!