menu.addIntentOptions 添加动态菜单
来源:互联网 发布:财务人员优化方案 编辑:程序博客网 时间:2024/05/16 16:09
每一个Activity都有一个默认的menu
android的一个activity可以再选中某项之后按menu键弹出特定的菜单,也就是动态菜单。动态菜单的实现是靠menu类中的addIntentOptions函数实现的,具体的声明如下:
int android.view.Menu.addIntentOptions(
int groupId, //菜单组的编号
int itemId, //itemId (可以让其为0)
int order, //菜单顺序,可以不考虑
ComponentName caller, //发起activity的activity
Intent[] specifics, // 以action+uri的具体方式来增加激活相应activity的菜单项
Intent intent, //以categroy+uri这种一般形式来增加激活相应activity的菜单项
int flags,
MenuItem[] outSpecificItems) //这个是返回的MenuItem值,对应以specifics方式匹配的菜单项
这个函数是用来动态产生option menu。
参数Intent和Specifics的区别是,一个用categroy+uri来匹配activity,一个用action+uri来匹配 activity。
下面以android sdk中notepad的例子来说明其用法。
来看这个例子中的NotesList.java 文件中的public boolean onPrepareOptionsMenu(Menu menu)函数,这个函数会在设定普通option menu菜单项的的onCreateOptionsMenu函数之后调用,这个函数的主要部分是如下代码:
1. Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());
2.
3. Intent[] specifics = new Intent[1];
4. specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
5. MenuItem[] items = new MenuItem[1];
6.
7. Intent intent = new Intent(null, uri);
8. intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
9. 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,Android比较了intent对象中和每个application所暴露的intent-filter中 的信息,来找到最合适的activity来处理调用者所指定的数据和操作。如果有某些 activity的<intent-filter>项的Category和data与 Intent.CATEGORY_ALTERNATIVE和相应的uri匹配,就为这些activity分别添加菜单项,菜单项的显示名称从相应activity的android:label项
得来。
下面可以做个试验,在AndroidMenifest.xml中新建一个activity MyAdd
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的layout和实现后,选中noteslist中的一项后,点 menu可以看到菜单中多出了一项,点击可以切换到该activity。
这是以函数中Intent匹配的菜单项,当然也可以用Specifics来匹配。下面示例删除掉MyAdd这个activity中的 <category android:name="android.intent.category.ALTERNATIVE" />,这时该activity已经与Intent不匹配了,再将onPrepareOptionsMenu 函数中的代码改成如下:
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];
选中一项点菜单会发现,动态菜单又回来了,不过这次是用Specific来匹配的。
- menu.addIntentOptions 添加动态菜单
- menu.addIntentOptions 添加动态菜单
- menu.addIntentOptions 添加动态菜单
- menu.addIntentOptions 添加动态菜单
- menu.addIntentOptions 添加动态菜单
- menu.addIntentOptions 添加动态菜单
- menu.addIntentOptions 添加动态菜单
- menu.addIntentOptions 添加动态菜单
- menu.addIntentOptions添加动态菜单
- menu.addIntentOptions 添加动态菜单----转
- android menu.addIntentOptions 添加动态菜单
- Android动态添加menu菜单
- Extjs动态添加子菜单:Ext.menu.Item 动态添加 Ext.menu.Menu
- Struts Menu动态菜单
- 动态创建Menu 菜单
- easyUI menu动态添加
- Ext:Ext.menu.Menu动态加载菜单
- Ext.menu.Menu动态生成的菜单
- 菜鸟之驱动开发4
- VC程序的SECTION(3):.text
- 云参考框架 (大图)
- vs2010+wdk7600+XXX环境配置,随后2步最重要,其他和网上搜的大同小异
- CInternetSession的简单使用
- menu.addIntentOptions 添加动态菜单
- 我在Salira的800天(2009.5.20~2011.7.29)-四.总结篇
- Explain plan Oracle优化工具安装及使用
- 【Qt】Web与本地应用的混合开发
- Sql Server 2008 R2 error:40 错误处理
- oracle中数据库名、数据库实例名、SID的区别
- 汇编语言的短语、句柄、素短语
- 树状数组
- 《火星人敏捷开发手册》 2011-08-18版本发布