有动作条中的两个单选项将无法正常工作

有动作条中的两个单选项将无法正常工作

本文介绍了有动作条中的两个单选项将无法正常工作,但附加一个弹出菜单,而不是也不管用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我写一个Android应用程序,用户必须选择的方式和内容,以显示在图表上。这些选项pssed中的两个单选择菜单组(单选按钮),这两者应该是从操作栏访问EX $ P $。

I am writing an Android app where the user must choose how and what to display on a graph. These options are expressed in two single-selection menu groups (radio buttons), both of which should be accessible from the action bar.

第一组工作正常。它在我的动作条XML的结尾是这样说:

The first group works fine. It's added at the end of my ActionBar XML like this:

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

当我添加第二个&LT;组&gt; 下面的第一个,但是,这两个合并成一个单一的选择列表。换句话说,从两个列表的选项都呈现在一起,如果我选择属于第一列表中的选项,我不能选择从第二个事情。

When I add a second <group> below the first one, however, the two merge into one single-selection list. In other words, the options from both lists are rendered together and if I choose an option pertaining to the first list, I cannot choose anything from the second.

相反,我想两个单独的列表的单选按钮。我的下一个想法是添加一个按钮的动作条,点击后,将推出的 。但是,当我按一下按钮,我得到一个 IllegalStateException异常,说我MenuPopupHelper不能使用没有锚。

Instead, I want two separate lists of radio buttons. My next idea was to add another button the ActionBar that, when clicked, would launch a pop-up menu. But when I click the button, I get an IllegalStateException, saying that my "MenuPopupHelper cannot be used without an anchor".

下面是我试图弹出菜单code:

Here is my attempted pop-up menu code:

在我的动作条XML:

<item android:id="@+id/menu_openothermenu"
  android:title="@string/openothermenustr"
  android:showAsAction="always" />

我的新菜单中的XML:

My new menu 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>

code在我的活动:

Code in my Activity:

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

如何创建两套辨认的菜单项,使得两个连接到动作条?

推荐答案

我发现了一个优雅的方式来解决这个问题,这是不幸的是没有的文件中。添加以下code到你的动作条菜单中的XML:

I found an elegant way to solve this that was unfortunately not in the documentation. Add the following code to your ActionBar menu 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>

没有额外的处理程序code或弹出菜单的实现是必要出现这样的菜单。

No extra handler code or popup menu implementation is necessary for such a menu to appear.

这篇关于有动作条中的两个单选项将无法正常工作,但附加一个弹出菜单,而不是也不管用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:38