The examples in this article describe the three kinds of menus for Android. Share to everyone for your reference. The specific analysis is as follows:
The Android menu is divided into three types: Option menu, context menu, submenu (Sub menu)
First. Options menu
When the user clicks the menu button on the device, the menu that triggers the event pop-up is the option menu. The option menu has a maximum of six, and more than six sixth automatically displays more options to display.
To create a method:
1. The Oncreateoptionsmenu (Menu menu) method that covers the activity is invoked when we first open the menu.
2, the Add () method of the Call menu is added (MenuItem) to call the MenuItem SetIcon () method to set the icon for the menu item.
3. When a menu item (MenuItem) is selected, the Onoptionsmenuselected () method that overrides Acitivy responds to the event.
Second, context menu
When the user presses the Activity page, the pop-up menu is called the context menu. We often right-click on Windows with the right mouse button to click the pop-up menu, which is the context menu.
1, covering the activity of the Oncreatecontextmenu () method, call the Menu Add method added menu item MenuItem
2, Overlay oncontextitemselected () method, response menu click event
3, call the Registerforcontextmenu () method, register the context menu for the view
Third, sub menu
A submenu is a menu that displays a grouping of the same features in multiple levels, such as "new", "open", "close", and so on in the Windows File menu.
Ways to create submenus
1, covering the activity of the Oncreateoptionsmenu () method, call the menu Addsubmenu () method to add a submenu item
2, call the submenu add () rice, add child menu items
3, Overlay oncreateitemselected () method, response menu click event
|
tatic Final int ITEM2 = Menu.first + 1; private static final int ITEM3 = Menu.first + 2; private static final int ITEM4 = Menu.first + 3; private static final int ITEM5 = Menu.first + 4; private static final int ITEM6 = Menu.first + 5; private static final int ITEM7 = Menu.first + 6; TextView MyTV; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); MyTV = (TextView) Findviewbyid (R.ID.MYTV); At this time, the context menu is set for MyTV, and the response function Registerforcontextmenu (MYTV) is long pressed TextView. @Override//Click on the menu button to respond to events public boolean Oncreateoptionsmenu (Menu menu) {/* Code for Option Menu Test//Add menu item//Public abstract Menu Itemadd (int groupId, int itemId, int order, charsequence title)//menu in Group, ID of menu, rank, display text//Add () method returns a MenuItem object. And SetIcon is the MenuItem method Menu.add (0, ITEM1, 0, "start"). SetIcon (R.drawable.ic_launcher); Menu.add (0, ITEM2, 0, "Start 1"); Menu.add (1, ITEM3, 0, "Start 2"); Menu.add (1, ITEM4, 0, "Start 3"); Menu.add (1, ITEM5, 0, "Start 4"); Menu.add (0, ITEM6, 0, "Start 5"); Menu.add (0, ITEM7, 0, "Start 6"); * */** * The following code is to add a submenu of the test Code///Add submenu submenu Subfile = Menu.addsubmenu ("file"); submenu Editfile = Menu.addsubmenu ("edit"); Add menu items for submenus Subfile.add (0, ITEM1, 0, "new"); Subfile.add (0, ITEM2, 0, "open"); return true; Event public boolean onoptionsitemselected (MenuItem item) {*////////Here is a container, and MenuItem is equivalent to a container in which the menu is selected. Something to hold Switch (Item.getitemid ()) {case ITEM1://Set the title of the activity Settitle ("Start Game 1"); break, Case Item2:settitle ("Start Game 2"); Break Case Item3:settitle ("Start Game 3"); Break Case Item4:settitle ("Start Game 4"); Break Case Item5:settitle ("Start Game 5"); Break Case Item6:settitle ("Start Game 6"); Break Case Item7:settitle ("Start Game 7"); Break * * */** * Submenu Item response code/switch (Item.getitemid ()) {case ITEM1://Set the title of the Activity Settitle ("New file"), break, Case Item2:set Title ("Open file"); Break return true; @Override//Create context menu PuBlic void Oncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {//Add menu item to context menu//Note that the menu here is Contextme Nu menu.add (0, ITEM1, 0, "red background"); Menu.add (0, ITEM2, 0, "green background"); Menu.add (1, ITEM3, 0, "white background"); @Override public boolean oncontextitemselected (MenuItem item) {switch (Item.getitemid ()) {case ITEM1:myTV.setBackgrou Ndcolor (color.red); Break Case ITEM2:myTV.setBackgroundColor (Color.green); Break Case ITEM3:myTV.setBackgroundColor (Color.White); Break return true; } } |
I hope this article will help you with your Android program.