Android的可检查菜单项 [英] Android Checkable Menu Item

查看:129
本文介绍了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.

文档

在一个可检查的项目被选中时,系统调用的各个项目选择回调方法(如<一个href="http://d.android.com/reference/android/app/Activity.html#onOptionsItemSelected%28android.view.MenuItem%29"><$c$c>onOptionsItemSelected()).正是在这里,你必须设置复选框的状态,因为一个复选框或单选按钮不会自动改变其状态,可查询该项目的当前状态(因为它以前的用户选择了它),与<一个href="http://d.android.com/reference/android/view/MenuItem.html#isChecked%28%29"><$c$c>isChecked()然后设置的选中状态<一href="http://d.android.com/reference/android/view/MenuItem.html#setChecked%28boolean%29"><$c$c>setChecked().

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的可检查菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆