Android layout type Resource (3) --- menu

Source: Internet
Author: User


<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.