在ActionBar中有两个单选组不起作用,但附加一个弹出菜单也不起作用

Max*_*sky 5 android android-layout android-menu android-actionbar drop-down-menu

我正在编写一个Android应用程序,用户必须选择在图表上显示的方式和内容.这些选项以两个单选菜单组(单选按钮)表示,两者都可以从操作栏访问.

第一组工作正常.它添加在我的ActionBar XML的末尾,如下所示:

<group android:checkableBehavior="single" android:showAsAction="never" >
    <item android:id="@+id/menu_choice_1" android:title="Choice 1" />
    <item android:id="@+id/menu_choice_2" android:title="Choice 2" android:checked="true"/>
</group>
Run Code Online (Sandbox Code Playgroud)

<group>但是,当我在第一个下面添加第二个时,两个合并为一个单选列表.换句话说,两个列表中的选项一起呈现,如果我选择与第一个列表相关的选项,我就不能从第二个列表中选择任何内容.

相反,我想要两个单独的单选按钮列表.我的下一个想法是添加ActionBar的另一个按钮,当点击它时,将启动一个弹出菜单.但是当我点击按钮时,我得到一个IllegalStateException,说我的"MenuPopupHelper不能在没有锚点的情况下使用".

这是我尝试的弹出式菜单代码:

在我的ActionBar XML中:

<item android:id="@+id/menu_openothermenu"
  android:title="@string/openothermenustr"
  android:showAsAction="always" />
Run Code Online (Sandbox Code Playgroud)

我的新菜单XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:id="@+id/menu_2_choice_1" android:title="@string/2_choice_1" />
        <item android:id="@+id/menu_2_choice_2" android:title="@string/2_choice_2" android:checked="true"/>
    </group>
</menu>
Run Code Online (Sandbox Code Playgroud)

我的活动代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor;

    switch (item.getItemId()) {
    case R.id.openothermenu:
        Menu m = (Menu) findViewById(R.menu.other_menu);
        PopupMenu popup = new PopupMenu(this, findViewById(R.menu.main_menu));
        popup.setOnMenuItemClickListener(this);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.other_menu, popup.getMenu());
        /* This commented block doesn't work either, and prevents execution
        // Restore saved chosen value
        int chosen = settings.getInt(MENU_2_PREFS, -1);
        switch(chosen)
        {
            case 1:
                m.findItem(R.id.menu_1_choice_1).setChecked(true);
                updateVisibleThings();
                break;
            default:
            case 2:
                m.findItem(R.id.menu_2_choice_2).setChecked(true);
                updateOtherVisibleThings();
                break;
        }
        */
        popup.show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor;

    switch(item.getItemId()) {
    case R.id.menu_2_choice_1:
        if (item.isChecked()) item.setChecked(false);
        else item.setChecked(true);
        updateVisibleThings();

        // save chosen setting
        editor = settings.edit();
        editor.putInt(MENU_2_PREFS, 1);
        editor.commit(); // Commit the edits!

        return true;
    case R.id.menu_2_choice_2:
        if (item.isChecked()) item.setChecked(false);
        else item.setChecked(true);
        updateOtherVisibleThings();

        // save chosen setting
        editor = settings.edit();
        editor.putInt(MENU_2_PREFS, 2);
        editor.commit(); // Commit the edits!

        return true;
    default:
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何创建两组可检查菜单项,以便两者都附加到ActionBar?

Max*_*sky 20

我找到了一种解决这个问题的优雅方法,遗憾的是,这种方法并没有出现 将以下代码添加到ActionBar菜单XML:

<item android:id="@+id/menu_openothermenu"
      android:title="@string/openothermenustr"
      android:showAsAction="always">
    <menu>
        <group android:checkableBehavior="single">
            <item android:id="@+id/menu_2_choice_1" android:title="@string/2_choice_1"  android:showAsAction="never" />
            <item android:id="@+id/menu_2_choice_2" android:title="@string/2_choice_2"  android:showAsAction="never" android:checked="true" />
        </group>
     </menu>
</item>
Run Code Online (Sandbox Code Playgroud)

这样的菜单不需要额外的处理程序代码或弹出菜单实现.