5

ユーザーがグラフに表示する方法と内容を選択する必要がある Android アプリを作成しています。これらのオプションは、2 つの単一選択メニュー グループ (ラジオ ボタン) で表現され、どちらもアクション バーからアクセスできる必要があります。

最初のグループは正常に動作します。次のように、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>

<group>ただし、最初のものの下に2 番目を追加すると、2 つが1 つの単一選択リストにマージされます。つまり、両方のリストのオプションが一緒にレンダリングされ、最初のリストに関連するオプションを選択すると、2 番目のリストからは何も選択できなくなります。

代わりに、ラジオ ボタンの2 つの個別のリストが必要です。私の次のアイデアは、クリックするとポップアップ メニューを起動する別のボタンを ActionBar に追加することでした。しかし、ボタンをクリックするIllegalStateExceptionと、「MenuPopupHelper はアンカーなしでは使用できません」というメッセージが表示されます。

これが私の試行されたポップアップメニューコードです:

私のActionBar XMLでは:

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

私の新しいメニュー 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>

私のアクティビティのコード:

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

両方が ActionBar にアタッチされるように、チェック可能なメニュー項目の 2 つのセットを作成するにはどうすればよいですか?

4

1 に答える 1

21

これを解決するエレガントな方法を見つけましたが、残念ながらドキュメントにはありませんでした。次のコードを 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>

このようなメニューを表示するために、追加のハンドラー コードやポップアップ メニューの実装は必要ありません。

于 2012-09-21T02:44:50.747 に答える