<Menu xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: icon = "@ drawable/ic_launcher" android: title = "File"> <menu> <group android: id = "@ + id/noncheckable_group" android: checkableBehavior = "none"> <item android: id = "@ + id/newFile" android: title = "New" android: alphabeticShortcut = "n"> </item> <item android: id = "@ + id/openFile" android: title = "Open" android: alphabeticShortcut = "o"> </item> <item android: id = "@ + id/saveFile" android: title = "Save" android: alphabeticShortcut = "s"> </item> </group> </menu> </item> <item android: title = "Edit" android: icon = "@ drawable/ic_launcher"> <menu> <group android: id = "@ + id/edit_group" android: checkableBehavior = "single"> <item android: id = "@ + id/cut" android: title = "Cut"> </item> <item android: id = "@ + id/past" android: title = "Past"> </item> </group> </menu> </item> <item android: title = "Help" android: icon = "@ drawable/ic_launcher"> <menu> <group android: id = "@ + id/help_group"> <item android: id = "@ + id/about" android: title = "About"> </item> <item android: id = "@ + id/exit" android: title = "Exit"> </item> </group> </menu> </item> </menu>
Public class Test_Menu extends Activity {private MenuInflater mi; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. test_menu); mi = new MenuInflater (this) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {mi. inflate (R. menu. menu, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {switch (item. getI TemId () {case R. id. about: aboutAlert ("This example shows how to use Xml menu resources to define menus! "); Break; case R. id. exit: exitAlert (" are you sure you want to exit? "); Break;} return super. onOptionsItemSelected (item);} private void exitAlert (String msg) {// instantiate AlertDialog. builderAlertDialog. builder builder = new AlertDialog. builder (this); // set the display information builder. setMessage (msg ). setCancelable (false) // click OK. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// end Activityfinish () ;}// cancel button }). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {return ;}}); // create dialog box AlertDialog alert = builder. create (); // displayed dialog box alert. show ();} private void aboutAlert (String msg) {// instantiate AlertDialog. builderAlertDialog. builder builder = new AlertDialog. builder (this); // set the display information builder. setMessage (msg ). setCancelable (false) // click OK. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {}}); // create dialog box AlertDialog alert = builder. create (); // displayed dialog box alert. show ();}}
Public class Option_Menu extends Activity {// The Menu item ID constant private static final int Item_Start = Menu. FIRST; private static final int Item_Exit = Menu. FIRST + 1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. option_menu) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {menu. add (0, Item_Start, 0, "Start"); menu. add (0, Item_Exit, 0 ," Exit "); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {// menu item 1 is selected case Item_Start: // This Activity title setTitle ("Start game"); break; // select case Item_Exit: setTitle ("exit! "); Break;} return true ;}}
Source: http://blog.csdn.net/cl05300629/article/details/17717573 Author: Standing