Options Menu
To create the Options menu:
1, covering the activity of the Oncreateoptionmenu (Menu menu) method, when the menu is opened for the first time call
2. The Add () method of menu is called and the SetIcon () method of MenuItem can be called to set the icon for the menu item (MenuItem).
3. When the menu item (MenuItem) is selected, cover the activity's onoptionsitemselected (MenuItem item) to respond to the event
Public classMainactivityextendsActivity {//define some variables, and use them directly behind them. Private Static Final intStart_item =Menu.first; Private Static Final intOver_item = Menu.first + 1; Private Static Final intSet_item1 = Menu.first + 2; Private Static Final intSET_ITEM2 = Menu.first + 3; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//Add menu item (group ID, menu item ID, sort, title)Menu.add (0, Start_item, 100, "Start"); Menu.add (0, Over_item, 200, "End"); //Add a sub-menusubmenu sub1 = Menu.addsubmenu ("Settings"); Sub1.add (1, SET_ITEM1, 300, "Set sound"); Sub1.add (1, SET_ITEM2, 400, "set desktop"); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseStart_item://Prompt InformationToast.maketext ( This, "Start", Toast.length_short). Show (); Break; CaseOver_item://Prompt InformationToast.maketext ( This, "End", Toast.length_short). Show (); Break; Caseset_item1://Prompt InformationToast.maketext ( This, "Sound Settings", Toast.length_short). Show (); Break; Caseset_item2://Prompt InformationToast.maketext ( This, "Set up desktop", Toast.length_short). Show (); Break; default: Break; } return Super. onoptionsitemselected (item); }}
Context Menu
As the name implies, it is related to context (environment). The idea is similar to the right-click Popup shortcut menu in Windows. The operation takes a long time to hold down a certain stuff.
Steps to create a context menu:
1. The Oncreatecontextmenu (Menu menu) method, which overrides the activity, invokes the Add () method of menu and adds the item (MenuItem).
2. oncontextitemselected (MenuItem iitem) covering the activity to respond to the event.
3. Call the Registerforcontextmenu () method to register the context menu for the view.
Public classMainActivity2extendsActivity {PrivateButton btn1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.main2); //Get Button ObjectBTN1 =(Button) Findviewbyid (R.id.button1); //Registering the context menu on a buttonRegisterforcontextmenu (BTN1); } //Create event methods for context menus@Override Public voidOncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {Super. Oncreatecontextmenu (menu, V, menuinfo); Getmenuinflater (). Inflate (r.menu.menu_main2, menu); } //context menu item trigger event@Override Public Booleanoncontextitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.start:toast.maketext ( This, "Start ... ", Toast.length_short). Show (); Break; CaseR.id.over:toast.maketext ( This, "End ... ", Toast.length_short). Show (); Break; default: Break; } return Super. oncontextitemselected (item); }}
Popup Menu
Pop-up menu.
A pop-up menu is a modal menu docked on a view. If there is space below the View object, the pop-up menu will appear below the docking object, otherwise it will appear above. This is very useful.
Public classMainActivity3extendsActivityImplementsonmenuitemclicklistener{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.main3); } Public voidShowPopupMenu (View v) {//Create pop-up menu objects (minimum version)PopupMenu popup =NewPopupMenu ( This, V); //get the menu fillerMenuinflater Inflater =Popup.getmenuinflater (); //Fill Menuinflater.inflate (r.menu.menu_main3, Popup.getmenu ()); //to set a click event for a menu itemPopup.setonmenuitemclicklistener ( This); Popup.show (); } //Click event Handling for pop-up menus@Override Public BooleanOnmenuitemclick (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.copy:toast.maketext ( This, "Copy ... ", Toast.length_long). Show (); Break; CaseR.id.del:toast.maketext ( This, "Delete ... ", Toast.length_long). Show (); Break; default: Break; } return false; }}
I'm the dividing line of the king of the Land Tiger.
Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01
Menu learning. zip
Reprint Please specify source: Http://www.cnblogs.com/yydcdut