menu.addIntentOptions 添加動態菜單
android的一個activity可以再選中某項之後按menu鍵彈出特定的菜單,也就是動態菜單。動態菜單的實現是靠menu類中的addIntentOptions函數實現的,具體的聲明如下:int android.view.Menu.addIntentOptions(
int groupId,
int itemId,
int order,
ComponentName caller,
Intent[] specifics,
Intent intent,
int flags,
MenuItem[] outSpecificItems)
這個函數是用來動態產生option menu的
函數參數分析:
1. groupid 就是菜單組的編號;
2. itemId (可以讓其為0)
3. order 菜單順序,可以不考慮
4. Caller 就是發起activity的activity
5. Specifics 以action+uri的具體方式來增加激活相應activity的菜單項
6. Intent 以categroy+uri這種一般形式來增加激活相應activity的菜單項
參數Intent和Specifics的區別是,一個用categroy+uri來匹配activity,一個用action+uri來匹配 activity。
8. outSpecificItems 這個是返回的MenuItem值,對應以specifics方式匹配的菜單項。
下麵以android sdk中notepad的例子來說明其用法。
來看這個例子中的NotesList.java 文件中的public boolean onPrepareOptionsMenu(Menu menu)函數,這個函數會在設定普通option menu菜單項的的onCreateOptionsMenu函數之後調用,這個函數的主要部分是如下代碼:
view plaincopy to clipboardprint?
1. Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());
2.
3.
4. Intent[] specifics = new Intent[1];
5. specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
6. MenuItem[] items = new MenuItem[1];
7.
8.
9. Intent intent = new Intent(null, uri);
10. intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
11. menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0,
12. items);
Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId()); Intent[] specifics = new Intent[1]; specifics[0] = new Intent(Intent.ACTION_EDIT, uri); MenuItem[] items = new MenuItem[1]; Intent intent = new Intent(null, uri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);
其中 ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId())會得到選中項的信息,這個信息將會作用匹配用的intent的
data部分。
specifics[0] = new Intent(Intent.ACTION_EDIT, uri)在這裏是這個意思:到androidMenifest.xml中去找activity, 如果有某個activity的<intent-
filter>項的action和data與Intent.ACTION_EDIT和相應的uri匹配,就為這個activity添加一個菜單項,菜單項的顯示名稱從相應 activity的
android:label項得來。
Intent intent = new Intent(null, uri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
是這個意思:到androidMenifest.xml中去找activity,如果有某些 activity的<intent-filter>項的Category和data與
Intent.CATEGORY_ALTERNATIVE和相應的uri匹配,就為這些activity分別添加菜單項,菜單項的顯示名稱從相應activity的android:label項
得來。
下麵可以做個試驗,在AndroidMenifest.xml中新建一個activity MyAdd
view plaincopy to clipboardprint?
1. <activity android:name="MyAdd" android:label="@string/title_myadd"
2. android:windowSoftInputMode="stateVisible">
3. <intent-filter android:label="@string/resolve_myadd">
4. <action android:name="com.android.notepad.action.MYADD" />
5. <category android:name="android.intent.category.ALTERNATIVE" />
6. <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
7. </intent-filter>
8. </activity>
<activity android:name="MyAdd" android:label="@string/title_myadd" android:windowSoftInputMode="stateVisible"> <intent-filter android:label="@string/resolve_myadd"> <action android:name="com.android.notepad.action.MYADD" /> <category android:name="android.intent.category.ALTERNATIVE" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter> </activity>
寫好該activity的layout和實現後,選中noteslist中的一項後,點 menu可以看到菜單中多出了一項,點擊可以切換到該activity。
這是以函數中Intent匹配的菜單項,當然也可以用Specifics來匹配。下麵示例:
刪除掉MyAdd這個activity中的 <category android:name="android.intent.category.ALTERNATIVE" />,這時該activity已經與Intent不匹配了,
再將onPrepareOptionsMenu 函數中的代碼改成如下:
view plaincopy to clipboardprint?
1. Intent[] specifics = new Intent[2];
2. specifics[0] = new Intent(Intent.ACTION_VIEW, uri);
3. specifics[1] = new Intent("com.android.notepad.action.MYADD",uri);
4. MenuItem[] items = new MenuItem[2];
Intent[] specifics = new Intent[2]; specifics[0] = new Intent(Intent.ACTION_VIEW, uri); specifics[1] = new Intent("com.android.notepad.action.MYADD",uri); MenuItem[] items = new MenuItem[2];
選中一項點菜會發現,動態菜單又回來了,不過這次是用Specific來匹配的。
最後更新:2017-04-02 06:51:58