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)
A 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
Three, 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, the Oncreateoptionsmenu () method that overwrites the activity, calls the Addsubmenu () method of the menu to add submenu items
2, Invokes submenu's Add () rice meal, adds submenu items
3, Overwrite oncreateitemselected () method, response menu click event
public class Main extends activity {//menu item ID///The integer constant in the menus class private static final int ITEM1 = Menu.first;
private static 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 ABST Ract menuitemadd (int groupId, int itemId, int order, charsequence title)//menu in Group, menu ID, sort sequence, displayed text//Add () method returns a me Nuitem 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; The event that is triggered when @Override//menu is selected is public boolean onoptionsitemselected (MenuItem Item) {/////This explains that menu is equivalent to a container, and MenuItem phase
When the container holds something switch (item.getitemid ()) {case ITEM1://Set the title Settitle of the Activity ("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 Settitle of the activity ("New file");
Break
Case Item2:settitle ("Open file");
Break
return true; @Override//Create context menu public void Oncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {//Up
Add menu items to the menu below//Note that the menu here is ContextMenu 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.set
BackgroundColor (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.