Android 操作栏可检查菜单项无法正常工作/显示? [英] Android action bar checkable menu item does not work/show properly?

查看:19
本文介绍了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="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>

这是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

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

这篇关于Android 操作栏可检查菜单项无法正常工作/显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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