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

问题描述

我有一个溢出菜单的工作程序。所有code在菜单的工作原理,但没有复选标记被显示在我点击一个单一的点击的,分组菜单项。

我做得根本错误的?我认为,这显示勾选的是自动到Android并且系统会帮我这个忙。安卓知道它是在一组,它知道,只有一个可以选择的,它知道我选择哪一个!所以.....机器人应该知道如何显示选中标记。

code

XML

 <菜单
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    工具:上下文=计算器>
    <组
        机器人:checkableBehavior =单
        机器人:可见=真>
        <项目
            机器人:ID =@ + ID / ui_simple
            机器人:orderInCategory =100
            应用程序:showAsAction =ifRoom | withText
            机器人:标题=常规
            机器人:可检查=真/>
        <项目
            机器人:ID =@ + ID / ui_doge
            机器人:orderInCategory =100
            应用程序:showAsAction =ifRoom | withText
            机器人:标题=公爵
            机器人:可检查=真/>
        <项目
            机器人:ID =@ + ID / ui_static
            机器人:orderInCategory =100
            应用程序:showAsAction =ifRoom | withText
            机器人:标题=静态
            机器人:可检查=真/>
    < /组>

    <项目的android:标题=显示模式
        机器人:orderInCategory =101>
        <菜单>
            <组机器人:checkableBehavior =单的Andr​​oid版本:可见=真>
                <项目
                    机器人:ID =@ + ID / mode_sign
                    机器人:orderInCategory =100
                    应用程序:showAsAction =collapseActionView
                    机器人:标题=符号表
                    机器人:可检查=真/>
                <项目
                    机器人:ID =@ + ID / mode_noun
                    机器人:orderInCategory =100
                    应用程序:showAsAction =collapseActionView
                    机器人:标题=名词形式
                    机器人:可检查=真/>
                <项目
                    机器人:ID =@ + ID / mode_verb
                    机器人:orderInCategory =100
                    应用程序:showAsAction =collapseActionView
                    机器人:标题=动词形式
                    机器人:可检查=真/>
            < /组>

        < /菜单>
    < /项目>

< /菜单>
 

UI

注:我已经尝试切换所有的破发 s到返回true ,但没有任何反应。

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){

    //处理动作栏项目点击这里。将操作栏
    //自动在主/向上按钮操作的点击,只要
    //你在AndroidManifest.xml中指定一个父活动。
    开关(item.getItemId()){

        案例R.id.ui_simple:
            startActivity(新意图(这一点,Calculator.class));
            打破;

        案例R.id.ui_doge:
            startActivity(新意图(这一点,CalculatorDoge.class));
            打破;

        案例R.id.ui_static:
            startActivity(新意图(这一点,CalculatorStatic.class));
            打破;

        案例R.id.mode_sign:显示= BinaryOperation.Display.SIGN;打破;
        案例R.id.mode_verb:显示= BinaryOperation.Display.VERB;打破;
        案例R.id.mode_noun:显示= BinaryOperation.Display.NOUN;打破;

    }

    返回super.onOptionsItemSelected(项目);

}
 

解决方案

虽然@Elltz提供了一个有价值的解决方案在code的一个问题,总共有2个问题,在code是。

问题1

不要设置在两个菜单集团和个人可检查的XML属性的菜单项秒。

因为在XML中,<组机器人:checkableBehavior =单那就说明单选按钮有需要的;因此,项目组中机器人:可检查=真

 <组
        机器人:checkableBehavior =单一<  - 只有在这里 - >
        机器人:可见=真>
        <项目
            机器人:ID =@ + ID / mode_sign
            机器人:orderInCategory =100
            应用程序:showAsAction =collapseActionView
            机器人:标题=符号表/>
        <项目
            机器人:ID =@ + ID / mode_noun
            机器人:orderInCategory =100
            应用程序:showAsAction =collapseActionView
            机器人:标题=名词形式/>
        <项目
            机器人:ID =@ + ID / mode_verb
            机器人:orderInCategory =100
            应用程序:showAsAction =collapseActionView
            机器人:标题=动词形式/>
    < /组>
 

问题2

此外, @Elltz提供了一个很好的解决方案;但是,它是稍微比它所需要更复杂的

单一选择,只有

 开关(item.getItemId()){

        案例R.id.ui_simple:startActivity(新意图(这一点,Calculator.class));返回true;
        案例R.id.ui_doge:startActivity(新意图(这一点,CalculatorDoge.class));返回true;
        案例R.id.ui_static:startActivity(新意图(这一点,CalculatorStatic.class));返回true;

        //单选择 - 单选按钮
        案例R.id.mode_sign:
            item.setChecked(真正的); //只有这个....这里
            显示器= BinaryOperation.Display.SIGN;
            返回true;

        案例R.id.mode_verb:
            item.setChecked(真正的); // 这里
            显示器= BinaryOperation.Display.VERB;
            返回true;

        案例R.id.mode_noun:
            item.setChecked(真正的); // 和这里
            显示器= BinaryOperation.Display.NOUN;
            返回true;

        默认值:返回super.onOptionsItemSelected(项目);
    }
 

对于单个或多个选择

  //单个或多个选择 - 单选按钮或复选框
    开关(item.getItemId()){

        案例R.id.ui_simple:startActivity(新意图(这一点,Calculator.class));返回true;
        案例R.id.ui_doge:startActivity(新意图(这一点,CalculatorDoge.class));返回true;
        案例R.id.ui_static:startActivity(新意图(这一点,CalculatorStatic.class));返回true;

        案例R.id.mode_sign:
            item.setChecked(item.isChecked()!); //像这样...这里
            显示器= BinaryOperation.Display.SIGN;
            返回true;

        案例R.id.mode_verb:
            item.setChecked(item.isChecked()!); // 这里
            显示器= BinaryOperation.Display.VERB;
            返回true;

        案例R.id.mode_noun:
            item.setChecked(item.isChecked()!); // 和这里
            显示器= BinaryOperation.Display.NOUN;
            返回true;

        默认值:返回super.onOptionsItemSelected(项目);
    }
 

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.

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.

Code

XML

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

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);

}
解决方案

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

Problem 1

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

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>

Problem 2

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

For Single Selection Only

    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);
    }

For Single OR Multi Selection

    // 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);
    }

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

08-30 19:44