Android Sample NotePad (二)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// This is our one standard application action -- inserting a
// new note into the list.
menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert)
.setShortcut('3', 'a')
.setIcon(android.R.drawable.ic_menu_add);
// 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;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
Log.e(TAG, "bad menuInfo", e);
return;
}
Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
if (cursor == null) {
// For some reason the requested item isn't available, do nothing
return;
}
// Setup the menu header
menu.setHeaderTitle(cursor.getString(COLUMN_INDEX_TITLE));
// Add a menu item to delete the note
menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_delete);
}
关于android中的menu,主要有三种:
1. option menu, 当按下device的menu键时触发, 调用onCreateOptionsMenu方法一次,android 支持6个option menu items, 更多的item需要用more 来得到。 option menu 可以
用icon, 但不能用checkbox。
2. Contextmenu, 在屏幕上press and hold 一段时间后触发,
3. submenu, submenu不可以再有submenu。
option menu 一般的实现方法是在resource里面定义menu.xml, 订好menu item 的layout, 在onCreateOptionsMenu 方法中利用MenuInflater 来显示Menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
在notepad程序中,采用的是调用Menu 的add方法实时的添加Menu.
menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert) , 第一个参数是group id, 第二个是Menu id, 第三个是Order, 第四个是资源id.
最后三句是比较难理解的地方:
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);
简单来讲,他的作用是根据当前device的activity的情况,动态的在程序中添加menu. 以本例来说,android会search所有在device中的activity, 看有没有activity 符合intent 定义
的情况(category 是alternative), 如果找到, 就在notepad的option menu 中添加对应的menu. 为什么要动态的加? 因为不同的device情况不同,可能有的device有符合条件的
activity,有的device则没有。
To add menu items based on available activities that accept an Intent:
- Define an Intent with the category
CATEGORY_ALTERNATIVEand/orCATEGORY_SELECTED_ALTERNATIVE, plus any other requirements. - Call
Menu.addIntentOptions(). Android then searches for any applications that can perform the Intent and adds them to your menu.
If there are no applications installed that satisfy the Intent, then no menu items are added.
Note: CATEGORY_SELECTED_ALTERNATIVE is used to handle the currently selected element on the screen. So, it should only be used when creating a Menu in onCreateContextMenu().
for example:
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
// Create an Intent that describes the requirements to fulfill, to be included
// in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE.
Intent intent = new Intent(null, dataUri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
// Search and populate the menu with acceptable offering applications.
menu.addIntentOptions(
R.id.intent_group, // Menu group to which new items will be added
0, // Unique item ID (none)
0, // Order for the items (none)
this.getComponentName(), // The current Activity name
null, // Specific items to place first (none)
intent, // Intent created above that describes our requirements
0, // Additional flags to control items (none)
null); // Array of MenuItems that correlate to specific items (none)
return true;
}
To be included in other application menus, you need to define an Intent filter as usual, but be sure to include the CATEGORY_ALTERNATIVE and/or CATEGORY_SELECTED_ALTERNATIVE values for the Intent filter category. For example:
<intent-filter label="Resize Image">
...
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
...
</intent-filter>
更多关于动态menu的讨论可以参考:
http://www.cnblogs.com/phinecos/archive/2009/08/27/1554955.html
浙公网安备 33010602011771号