Google が提供するメモ帳アプリに基づいて簡単なアプリケーションを作成しています。私の最初のステップの 1 つは、可能であれば XML メニューを使用するようにアプリを変換することです。メイン アクティビティであるノート リストでは、MenuInflater を使用して、デフォルトの「作成」メニュー オプションを表示しています。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// menu initialization, use the baseline menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.noteslist, menu);
// generate any additional actions that can be performed on the
// overall list. In a normal install, there are no additional
// actions found here, but this allows other applications to extend
// our menu with their own actions.
Intent intent = new Intent(null, getIntent().getData());
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, new ComponentName(this, NotesList.class), null, intent, 0, null);
return true;
}
noteslist.xml の場合:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/compose_note"
android:icon="@drawable/ic_menu_compose"
android:title="@string/menu_compose"
android:alphabeticShortcut="c"
android:numericShortcut="3" />
</menu>
すべて正常に動作します。ここで、例に従って (また、リストで項目が選択されていない場合にインテント オプションを追加しようとする例として変更されています)、リストに項目があり、そのうちの 1 つが選択されている場合は、いくつかの追加オプションを追加したいと考えています。 :
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// determine if we have any items in the list via the ListAdapter
final boolean haveItems = (getListAdapter().getCount() > 0);
// do we have items?
if (haveItems) {
// there are items, check if any are selected
//Toast.makeText(getApplicationContext(), "position: " + getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
if (getSelectedItemPosition() >= 0) {
// an item is selected, add the intents for one of our list items to the menu
Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());
// build menu on the fly... always starts with the EDIT action
Intent[] specifics = new Intent[1];
specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
MenuItem[] items = new MenuItem[1];
// now add additional CATEGORY_ALTERNATIVE intent-based actions, (see the manifest)
Intent intent = new Intent(null, uri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);
// finally, add a shortcut to the edit menu item
if (items[0] != null) {
items[0].setShortcut('1', 'e');
}
}
} else {
menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
}
return true;
}
最後に、参照されている AndroidManifest.xml は、デモ アプリのデフォルトから変更されていません。
http://developer.android.com/resources/samples/NotePad/AndroidManifest.html
だから、私は2つの質問があります:
1) アイテムが選択されたときに onPrepareOptionsMenu() によって生成された結果のメニュー、[メモの編集] と [タイトルの編集] は、デフォルトのアイコンを使用し、割り当てられたショートカットはありません。Android:icon="" を介してインテント フィルターに別のアイコンを設定することはできますが、アルファベットと数字のショートカットを割り当てることはできません...これらを指定したいと思いますが、方法があることを望んでいました。これらのメニュー項目を XML で定義し、intent-filter によって識別されてアプリに取り込まれる場合は、XML をプルして何らかの方法でインフレート/インポートします。提案や指針はありますか?
2) onCreateOptionsMenu() で、CATEGORY_ALTERNATIVE を指定した addIntentOptions() 呼び出しが、インテント フィルターが category.ALTERNATIVE に設定されたアクティビティをメニューに追加しないのはなぜですか (この場合、追加しないのは正しい動作です。 onCreateOptionsMenu() と onPrepareOptionsMenu() の addIntentOptions() への実質的に同じ呼び出しは、異なるメニューになります)。