Android contains a variety of menus, in this case the Options menu and the context menu.
1. Options Menu
In Android, developers can deploy the menu they want to add in an XML document, and invoke it in the background.
1 <Menuxmlns:android= "Http://schemas.android.com/apk/res/android" >2 3 <Item4 Android:id= "@+id/action_add"5 Android:title= "Add"6 android:showasaction= "Always|withtext"/>7 <Item8 Android:id= "@+id/action_save"9 Android:title= "Save"Ten android:showasaction= "Ifroom"/> One <Item A Android:id= "@+id/action_discard" - Android:title= "Delete" - android:showasaction= "Ifroom"/> the <Item - Android:id= "@+id/action_delete" - Android:title= "Delete" - android:showasaction= "Ifroom"/> + <Item - Android:id= "@+id/action_undo" + Android:title= "undo" A android:showasaction= "Ifroom"/> at </Menu>
Menu.xml
In XML we define a few common menus, add, delete, save, etc. Define menus in XML by defining them in <menu/>. <item/> is a menu item. Android can have up to six menus, beyond which the Android system will automatically put out more menu items.
1 Public BooleanOncreateoptionsmenu (Menu menu) {2Inflate the menu; ThisAdds items to the action Barifit is present.3Menuinflater Inflater =Getmenuinflater ();4 inflater.inflate (R.menu.main, menu);5Moptionsmenu =menu;6 7 return Super. Oncreateoptionsmenu (menu);8}Menu Display
Override the Oncreateoptionsmenu method, which calls the already defined Menu.xml to display the menu item we just defined.
In development, the menu is also inseparable from events. Use Onoptionsitemclick to listen for selected events on the menu.
1 Public Booleanonoptionsitemselected (MenuItem menu)2 {3 4 Switch(Menu.getitemid ())5 {6 CaseR.id.action_add:7Toast.maketext ( This, "You clicked Add", Toast.length_long). Show ();8 return true;9 CaseR.id.action_delete:TenToast.maketext ( This, "You clicked Delete", Toast.length_long). Show (); One return true; A CaseR.id.action_save: -Toast.maketext ( This, "You clicked Save", Toast.length_long). Show (); - return true; the CaseR.id.action_undo: -Toast.maketext ( This, "You clicked Undo.", Toast.length_long). Show (); - return true; - default: + return Super. onoptionsitemselected (menu); - } + A at -}Menu Events
Of course, developers can also add menus dynamically, without having to pre-write each menu item in the XML.
Moptionsmenu.add (1, 1, 1, "add"); the first parameter: the group ID of the menu item, the second argument: the ID of the menu item, and the third parameter: the id,android of the order in which the menus are displayed is left-to-right, top-to-bottom, sorted by ID ; The fourth parameter: the title of a menu item
2. Sub-menu and context menu
In Android, submenus and menus are the same level and can be submenu.add by adding submenus. Add a submenu with a check box
1 Public BooleanOncreateoptionsmenu (Menu menu) {2 //inflate the menu; This adds items to the action bar if it is present.3 //Menuinflater inflater = Getmenuinflater ();4 //inflater.inflate (R.menu.main, menu);5 //moptionsmenu = menu;6submenu Filesubmenu=menu.addsubmenu (1,1,2, "file");7MenuItem Newmenu=filesubmenu.add (1,2,2, "new");8Newmenu.setcheckable (true);9MenuItem Openmenu=filesubmenu.add (2,3,3, "open");TenFilesubmenu.setgroupcheckable (2,true,true); One return Super. Oncreateoptionsmenu (menu); A}Sub-menu
The context menu can be associated with any view, and it must be associated with a view, such as button, TextView, and so on.
1 Public voidOncreatecontextmenu (ContextMenu menu,view view,contextmenuinfo menuinof)2 3 {4 Super. Oncreatecontextmenu (menu, view, menuinof);5Menu.add ("context menu");6Menu.add (0,item1,menu.none, "Zoom In"). Setcheckable (true). setchecked (true);7Menu.add (20,item2,menu.none, "Zoom Out");8Menu.add (20,item3,menu.none, "Pan");9Menu.setgroupcheckable (20,true,true);Tensubmenu Sub=menu.addsubmenu (0,4,menu.none, "Sub-menu"); OneSub.add ("Sub-menu 1"); ASub.add ("Sub-menu 2"); -}context Menu
A submenu can be added to the context menu. The context menu is added and needs to be registered.
Button button9= (button) Findviewbyid (R.ID.BUTTON9);
Registerforcontextmenu (BUTTON9);
This defines the context menu that is registered in the Button9.
If you want to implement the monitoring of the context menu selection time, the Oncontextitemselected method is implemented.
1 Public Booleanoncontextitemselected (MenuItem item) {2 Switch(Item.getitemid ()) {3 CaseITEM1:4 //Add the processing code here5Toast.maketext ( This, "Click to enlarge", 5000). Show ();6 return true;7 CaseITEM2:8 //Add the processing code here9Toast.maketext ( This, "clicked Zoom Out", 5000). Show ();Ten Break; One CaseITEM3: A //Add the processing code here - - Break; the } - return true; -}context Menu Events