- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@+id/item1"
- android:title="Item 1"
- android:icon="@drawable/icon"
- android:checkable="true"
- android:checked="false"
- />
- <group android:id="@+id/group_1"
- android:checkableBehavior="single">
- <item android:id="@+id/group_item1"
- android:title="Item 1 in group"
- />
- <item android:id="@+id/group_item2"
- android:title="Item 2 in group"
- android:checked="true"
- />
- </group>
- <item android:id="@+id/submenu"
- android:title="Sub Menu">
- <menu>
- <item android:id="@+id/submenu_item"
- android:title="Sub Menu Item"
- />
- </menu>
- </item>
- <item android:id="@+id/item3"
- android:title="item 3"
- android:checkable="true"
- android:checked="true"
- />
- </menu>
- public void onCreate(Bundle savedInstanceState) {
- ...
- registerForContextMenu(editText);
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- super.onCreateContextMenu(menu, v, menuInfo);
- getMenuInflater().inflate(R.menu.menu1, menu);
- }
- private static final int MENU_GROUPITEM1 = Menu.FIRST + 8;
- private static final int MENU_GROUPITEM2 = Menu.FIRST + 9;
- private static final int MENU_ITEM1 = Menu.FIRST + 10;
- public void onCreate(Bundle savedInstanceState) {
- ...
- registerForContextMenu(findViewById(R.id.edittext));
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- super.onCreateContextMenu(menu, v, menuInfo);
- menu.add(1,MENU_ITEM1,Menu.NONE, "Item 1").setCheckable(true).setChecked(false);
- // Group ID
- int groupId = 0;
- // The order position of the item
- int menuItemOrder = Menu.NONE;
- menu.add(groupId, MENU_GROUPITEM1, menuItemOrder, "Item 1 in group");
- menu.add(groupId, MENU_GROUPITEM2, menuItemOrder, "Item 2 in group")
- .setChecked(true);
- menu.setGroupCheckable(groupId, true, true); //这句要写在group item的最后
- SubMenu subMenu = menu.addSubMenu("Sub Menu 1");
- subMenu.add("Sub Menu Item")
- .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
- @Override
- public boolean onMenuItemClick(MenuItem item) {
- Toast.makeText(HelloDemo.this,
- "Sub Menu Item selected",
- Toast.LENGTH_SHORT).show();
- return true; //true表示完成当前item的click处理,不再传递到父类处理
- }
- });
- menu.add("Item 3").setCheckable(true).setChecked(true);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Group ID
- int groupId = 0;
- // The order position of the item
- int menuItemOrder = Menu.NONE;
- menu.add(groupId, MENU_COPY, menuItemOrder, "Copy")
- .setIcon(R.drawable.icon);
- menu.add(groupId, MENU_EDIT, menuItemOrder, "Edit");
- menu.add(groupId, MENU_PASTE, menuItemOrder, "Paste");
- menu.add(groupId, MENU_DELETE, menuItemOrder, "Delete");
- menu.add(groupId, MENU_OK, menuItemOrder, "Ok");
- menu.add(groupId, MENU_CANCEL, menuItemOrder, "Cancel");
- menu.add(groupId, MENU_TEST, menuItemOrder, "Test");
- menu.add(groupId, MENU_DEMO, menuItemOrder, "Demo");
- // .setIcon(R.drawable.icon); more expand menu 不支持icon, setIcon不会报错,但运行时还是看不到icon的
- //return super.onCreateOptionsMenu(menu);
- return true; //true表示要显示menu; false表示不显示menu
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch(item.getItemId()){
- case MENU_COPY:
- Toast.makeText(this, "Copy Item selected", Toast.LENGTH_SHORT).show();
- break;
- default: break;
- }
- return false;//false表示继续传递到父类处理
- }
本文出自 “学习Android” 博客,请务必保留此出处http://android.blog.51cto.com/268543/306424