Menu Settings
Covers two methods Oncreateoptionsmenu (Menu menu), onoptionsitemselected (MenuItem Item)
Oncreateoptionsmenu: Create a menu in this function
Onoptionsitemselected: Called when a menu is selected, using switch (Item.getitemid ()) to determine
Parameter 1. Grouping 2.ID 3. Sorting order 4. Title
Menu.add (1, 1, 0, "settings"). SetIcon (r.drawable.setting);
Menu.add (1, 2, 1, "account"). SetIcon (R.drawable.switchuser);
Menu.add (1, 3, 2, "official"). SetIcon (R.drawable.officialweibo);
Menu.add (2, 4, 3, "views"). SetIcon (r.drawable.comment);
Menu.add (2, 5, 4, "about"). SetIcon (R.drawable.aboutweibo);
Menu.add (2, 6, 5, "exit"). SetIcon (R.drawable.menu_exit);
Popup context menu, need Registerforcontextmenu (View v) to register
ListView LV = new Lisrview (this);
Registerforcontextmenu (LV);//Register a ListView context menu
The effect is that you can press and hold an item in the ListView, and a message box appears with a menu option in the box
The following lm.id is the row ID used to display the child view of the context menu. That is, the return value of the Baseadapter method Getitemid (int position) is obtained.
Trigger an item on a context menu
@Override
public boolean oncontextitemselected (MenuItem item) {
TODO auto-generated Method Stub
Adaptercontextmenuinfo lm= (Adaptercontextmenuinfo) item.getmenuinfo ();
Switch (Item.getitemid ()) {
Case 1://Forwarding
Break
Case 2://Comment
Break
Case 3://Collection
Break
Case 4://Viewing information
Break
}
Toast.maketext (this, Item.gettitle (), +). Show ();
return super.oncontextitemselected (item);
}
Create a context Menu
@Override
public void Oncreatecontextmenu (ContextMenu menu, View V,
Contextmenuinfo menuinfo) {
TODO auto-generated Method Stub
Super.oncreatecontextmenu (menu, V, menuinfo);
Adaptercontextmenuinfo lm= (adaptercontextmenuinfo) menuinfo;
System.out.println ("+lm.id+" Item, popup menu registration succeeded ");
Menu.setheadertitle ("+lm.id+" Item's popup menu ");
Menu.add (1, 1, 1, "forward");
Menu.add (1, 2, 2, "comments");
Menu.add (1, 3, 3, "collection");
Menu.add (1, 4, 4, "View information
}
Android-context Menu Creation-Your heart