组菜单项有效但不显示复选标记 [英] Group menu items work but don't display checkmark

查看:22
本文介绍了组菜单项有效但不显示复选标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有溢出菜单的工作应用程序.菜单中的所有代码都有效,但在我单击可单击的分组菜单项后,没有显示复选标记.

I have a working app with an overflow menu. All the code in the menu works, but no checkmarks are being shown after I click on a single-clickable, grouped menu item.

我做错了什么吗?我认为此复选标记的显示对 Android 来说是自动的,并且系统会为我执行此操作.Android 知道它在一个组中,它知道只能选择一个,并且它知道我选择了哪一个!所以..... Android 应该知道如何显示复选标记.

Am I doing something fundamentally wrong? I thought that this displaying of the checkmark was automatic to Android and that the system would do this for me. Android knows it is in a group, it knows that only one can be selected, and it knows which one I selected! So..... Android should know how to display the checkmarks.

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Calculator">
    <group
        android:checkableBehavior="single"
        android:visible="true">
        <item
            android:id="@+id/ui_simple"
            android:orderInCategory="100"
            app:showAsAction="ifRoom|withText"
            android:title="Regular"
            android:checkable="true"/>
        <item
            android:id="@+id/ui_doge"
            android:orderInCategory="100"
            app:showAsAction="ifRoom|withText"
            android:title="Doge"
            android:checkable="true"/>
        <item
            android:id="@+id/ui_static"
            android:orderInCategory="100"
            app:showAsAction="ifRoom|withText"
            android:title="Static"
            android:checkable="true"/>
    </group>

    <item android:title="Display Mode"
        android:orderInCategory="101" >
        <menu>
            <group android:checkableBehavior="single" android:visible="true"   >
                <item
                    android:id="@+id/mode_sign"
                    android:orderInCategory="100"
                    app:showAsAction="collapseActionView"
                    android:title="Sign Form"
                    android:checkable="true"/>
                <item
                    android:id="@+id/mode_noun"
                    android:orderInCategory="100"
                    app:showAsAction="collapseActionView"
                    android:title="Noun Form"
                    android:checkable="true"/>
                <item
                    android:id="@+id/mode_verb"
                    android:orderInCategory="100"
                    app:showAsAction="collapseActionView"
                    android:title="Verb Form"
                    android:checkable="true"/>
            </group>

        </menu>
    </item>

</menu>

界面

注意:我已经尝试将所有 break 切换为 return true,但没有任何反应.

UI

Note: I have tried switching all the breaks to return true, but nothing happens.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {

        case R.id.ui_simple :
            startActivity(new Intent(this, Calculator.class));
            break;

        case R.id.ui_doge :
            startActivity(new Intent(this, CalculatorDoge.class));
            break;

        case R.id.ui_static :
            startActivity(new Intent(this, CalculatorStatic.class));
            break;

        case R.id.mode_sign : display = BinaryOperation.Display.SIGN; break;
        case R.id.mode_verb : display = BinaryOperation.Display.VERB; break;
        case R.id.mode_noun : display = BinaryOperation.Display.NOUN; break;

    }

    return super.onOptionsItemSelected(item);

}

推荐答案

虽然@Elltz 为代码中的一个问题提供了一个有价值的解决方案,但代码中总共有 2 个问题.

While @Elltz provides a valuable solution to a problem in the code, there are a total of 2 issues in the code.

不要在菜单组和单个MenuItem中设置可检查的XML属性.

Do not set a checkable XML attribute in both the Menu Group and the individual MenuItems.

因为在 XML 中有 <group android:checkableBehavior="single" 它表明 radio buttons 是需要的;因此,group 中的item 不应该有 android:checkable="true"

since in the XML there is <group android:checkableBehavior="single" it shows that radio buttons are desired; therefore, no item within the group should have android:checkable="true"

    <group 
        android:checkableBehavior="single"      <!-- ONLY HERE -->
        android:visible="true"   >
        <item
            android:id="@+id/mode_sign"
            android:orderInCategory="100"
            app:showAsAction="collapseActionView"
            android:title="Sign Form" />
        <item
            android:id="@+id/mode_noun"
            android:orderInCategory="100"
            app:showAsAction="collapseActionView"
            android:title="Noun Form"/>
        <item
            android:id="@+id/mode_verb"
            android:orderInCategory="100"
            app:showAsAction="collapseActionView"
            android:title="Verb Form"/>
    </group>

问题 2

同样,@Elltz 提供了一个很好的解决方案;然而,它比实际需要的要复杂一些.

Problem 2

Again, @Elltz provides a good solution; however, it is slightly more complex than what it needs to be.

仅限单选

    switch (item.getItemId()) {

        case R.id.ui_simple : startActivity(new Intent(this, Calculator.class)); return true;
        case R.id.ui_doge : startActivity(new Intent(this, CalculatorDoge.class)); return true;
        case R.id.ui_static : startActivity(new Intent(this, CalculatorStatic.class)); return true;

        // Single Selection - Radio Buttons
        case R.id.mode_sign :
            item.setChecked(true);    // ONLY THIS....HERE
            display = BinaryOperation.Display.SIGN;
            return true;

        case R.id.mode_verb :
            item.setChecked(true);    // HERE
            display = BinaryOperation.Display.VERB;
            return true;

        case R.id.mode_noun :
            item.setChecked(true);    // AND HERE
            display = BinaryOperation.Display.NOUN;
            return true;

        default : return super.onOptionsItemSelected(item);
    }

单选或多选

    // Single OR Multi Select - Radio Buttons or CheckBoxes
    switch (item.getItemId()) {

        case R.id.ui_simple : startActivity(new Intent(this, Calculator.class)); return true;
        case R.id.ui_doge : startActivity(new Intent(this, CalculatorDoge.class)); return true;
        case R.id.ui_static : startActivity(new Intent(this, CalculatorStatic.class)); return true;

        case R.id.mode_sign :
            item.setChecked(!item.isChecked());    // LIKE THIS...HERE
            display = BinaryOperation.Display.SIGN;
            return true;

        case R.id.mode_verb :
            item.setChecked(!item.isChecked());    // HERE
            display = BinaryOperation.Display.VERB;
            return true;

        case R.id.mode_noun :
            item.setChecked(!item.isChecked());    // AND HERE
            display = BinaryOperation.Display.NOUN;
            return true;

        default : return super.onOptionsItemSelected(item);
    }

这篇关于组菜单项有效但不显示复选标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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