Android的可检查菜单项

问题描述:

我在我的Andr​​oid应用程序下面的菜单布局:

I have the following menu layout in my Android app:

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

我的问题是第二个菜单项没有出现被辨认当我运行我的Andr​​oid模拟器的应用程序。应该有有关该项目的一个绿色的勾,对不对?为了表明其辨认。

My problem is that the second menu item doesn't appear to be "checkable" when I run my app in the Android emulator. There should be a green tick about the item, right? To indicate that its checkable.

我是不是做错了什么?

Am I doing something wrong?

布局看起来正确。但是,你必须检查并取消在code菜单项。

Layout looks right. But you must check and uncheck menu item in code.

文档

在一个可检查的项目被选中时,系统调用的各个项目选择回调方法(如onOptionsItemSelected()$c$c>).正是在这里,你必须设置复选框的状态,因为一个复选框或单选按钮不会自动改变其状态,可查询该项目的当前状态(因为它以前的用户选择了它),与isChecked()$c$c>然后设置的选中状态setChecked()$c$c>.

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