menu.addIntentOptions 添加动态菜单

原创 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" android:windowsoftinputmode="stateVisible"> 3. <intent-filter android:label="@string/resolve_myadd"> 4. <action android:name="com.android.notepad.action.MYADD"></action> 5. <category android:name="android.intent.category.ALTERNATIVE"></category> 6. <data android:mimetype="vnd.android.cursor.item/vnd.google.note"></data> 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"></action><category android:name="android.intent.category.ALTERNATIVE"></category><data android:mimetype="vnd.android.cursor.item/vnd.google.note"></data></intent-filter></activity> 写好该activity的layout和实现后,选中noteslist中的一项后,点menu可以看到菜单中多出了一项,点击可以切换到该activity。 这是以函数中Intent匹配的菜单项,当然也可以用Specifics来匹配。下面示例: 删除掉MyAdd这个activity中的 <category android:name="android.intent.category.ALTERNATIVE"></category>,这时该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来匹配的。</intent-filter></intent->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值