This article describes the implementation of the menu of Android programming. Share to everyone for your reference, specific as follows:
Options menu that appears when the User presses menu button buttons
Context menu when users hold down the screen for a long time, the menus displayed when the view is registered to display the contextual menu
submenu a submenu that pops up when a user presses an option on a menu
The above three kinds of menus are what we often say: Options menu, context menu and submenu.
A. Context Menu
1. Implement Oncreatecontextmenu to create the menu
@Override public
void Oncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {
//TODO auto-generated method Stub
super.oncreatecontextmenu (menu, V, menuinfo);
Load a defined layout menu.xml
menuinflater inflater = Getmenuinflater ();
Inflater.inflate (R.layout.menu, menu);
Or use code to implement menu add
menu.setheadertitle ("Long Press Menu-contextmenu");
Menu.add (0, 0, 0, "pop-up Long press Menu 0");
Menu.add (0, 1, 0, "pop-up Long press Menu 1");
}
2. Call this method when a menu option is clicked
@Override Public
Boolean oncontextitemselected (MenuItem item) {
//TODO auto-generated method stub
Adaptercontextmenuinfo infor = (adaptercontextmenuinfo) item.getmenuinfo ();
Switch (Item.getitemid ()) {case
R.id.help: Return
true;
Case r.id.addnew:
}
3. method to call when context menu is closed
@Override public
void oncontextmenuclosed (Menu menu) {
//TODO auto-generated method stub
Super.oncontextmenuclosed (menu);
For a context menu that has already been defined, it has to be registered on some controls so that when we hold the control long, we can jump out of the context menu. So how do you register the context menu on some controls, the first is a listener that implements ListView
Listview.setoncreatecontextmenulistener (New Oncreatecontextmenulistener () {
However, the context menu is also created in this method.
public void Oncreatecontextmenu (ContextMenu conmenu, view view, Contextmenuinfo info) {
Conmenu.setheadertitle (" ContextMenu ");
Conmenu.add (0, 0, 0, "Delete this favorite!");
/* Add as many context-menu-options as you want to. */
}
});
or call Registerforcontextmenu (THIS.LISTVIEW1) directly in this activity; This is registered to a ListView, so long press ListView can jump out of the context menu.
Two. Options Menu
It's simpler than that.
Just to realize oncreateoptionsmenu,onoptionsitemselected and onoptionsmenuclosed is OK.
Three. Submenu
Logically, the submenu is not difficult, indeed, when the implementation of the option menu, or context menu (in the context menu can also implement submenus, here we cite such an example), then the implementation of the submenu is the case. As the previous generation
Code:
@Override public
void Oncreatecontextmenu (ContextMenu menu, View V,
contextmenuinfo menuinfo) {
//TODO auto-generated method Stub
super.oncreatecontextmenu (menu, V, menuinfo);
Load a defined layout menu.xml
menuinflater inflater = Getmenuinflater ();
Inflater.inflate (R.layout.menu, menu);
Or use code to implement menu add
menu.setheadertitle ("Long Press Menu-contextmenu");
Menu.add (0, 0, 0, "pop-up Long press Menu 0");
Menu.add (0, 1, 0, "pop-up Long press Menu 1");
Here you can add the code
int base = menu that implements the submenu. A;
submenu SM = Menu.addsubmenu (base, base + 1, Menu.none, "submenu");
Sm.add (base, base + 2, base + 2, "sub item1");
Sm.add (base, base + 3, base + 3, "sub item2");
This way the menu is also implemented, for its Click event, as long as in the corresponding Options menu or context menu of the listener to judge the execution of the OK.
I hope this article will help you with the Android program.