Add a menu to activity
1. Create a new folder under the Res directory menu
Right-click Res, select New->folder,folder Name to write as menu
2. Create a new XML file under the new menu directory
Right-click menu, click New->android XML File, fill in the popup box with a name, such as main, click Finish
3. In the new Main.xml file, write the menu item you want item, as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Menuxmlns:android= "Http://schemas.android.com/apk/res/android" >3 <ItemAndroid:id= "@+id/add_item"4 Android:title= "Add"/>5 <ItemAndroid:id= "@+id/remove_item"6 Android:title= "Remove"/>7 </Menu>
4. Return to main activity
Rewrite two methods Oncreateoptionsmenu (Menu menu) and onoptionsitemselected (MenuItem item);
The first is to add a menu and the second is to respond when the corresponding menu item is clicked
Public BooleanOncreateoptionsmenu (Menu menu) {//TODO auto-generated Method Stubgetmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//TODO auto-generated Method Stub Switch(Item.getitemid ()) { CaseR.id.add_item:toast.maketext ( This, "ADD", Toast.length_short). Show (); Break; CaseR.id.remove_item:toast.maketext ( This, "REMOVE", Toast.length_short). Show (); Break; default: Break; } return true; }
5. Operation effect
My Android Learning experience 33