Android 可检查菜单项

标签 android android-menu android-checkbox

我的 Android 应用中有以下菜单布局:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1" 
          android:titleCondensed="Options"
          android:title="Highlight Options" 
          android:icon="@android:drawable/ic_menu_preferences" />

   <item android:id="@+id/item2" 
         android:titleCondensed="Persist"
         android:title="Persist" 
         android:icon="@android:drawable/ic_menu_preferences" 
         android:checkable="true" />
</menu>

我的问题是,当我在 Android 模拟器中运行我的应用程序时,第二个菜单项似乎不是“可检查的”。该项目应该有一个绿色的勾号,对吧?表明它是可检查的。

我做错了吗?

最佳答案

布局看起来不错。但是你必须在代码中选中和取消选中菜单项。

来自 documentation :

When a checkable item is selected, the system calls your respective item-selected callback method (such as onOptionsItemSelected()). It is here that you must set the state of the checkbox, because a checkbox or radio button does not change its state automatically. You can query the current state of the item (as it was before the user selected it) with isChecked() and then set the checked state with setChecked().

关于Android 可检查菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6239163/

相关文章:

android - CollapsingToolbarLayout 折叠时工具栏标题不显示

Android对话框昏暗动画

java - 如何在不破坏和重新创建 fragment 的情况下在 fragment 之间切换? (以类似静态的方式)

android - 覆盖android上的物理菜单按钮

android - 生成 apk 后 key 散列无效以及如何为许多设备解决此问题?

java - 我正在为 android 使用 Firebase Apple 登录

android - 如何在 TextView 中选择文本时覆盖 "Copy"、 "Share"和 "Select All"选项

android - 使用 sharedPreference 从多个 Activity 中传递和检索 bool 值

android - 消失前在复选框中显示勾号

android - 如何在多选 ListView 中保存勾选位置项的状态