Two Methods for Android to call menu

Source: Internet
Author: User

If you don't want to talk about anything, go directly to the Code:

Method 1:

Mainactivity. Java

Package COM. example. bluetoothchatv1; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. menu; import android. view. menuinflater; import android. view. menuitem; import android. widget. toast; public class mainactivity extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main);} // when you click menu, the system calls the oncreateoptionsmenu method of the current activity and transmits a menu object that implements a menu interface for you to use @ overridepublic Boolean oncreateoptionsmenu (menu) the four parameters of the {/** add () method are: * 1, group, and menu if no group is set. none, * 2, ID, this is very important, Android determines the different menus * 3, order according to this ID, the menu is now determined by the size of this parameter * 4. text. The displayed text */menu of the menu. add (menu. none, menu. first + 1, 5, "delete "). seticon (Android. r. drawable. ic_menu_delete); // seticon () method is used to set the menu icon. Here, the built-in icons are used. Students should pay attention to it and use // android. resources starting with R are provided by the system. The resources we provide ourselves are menus starting with R. add (menu. none, menu. first + 2, 2, "save "). seticon (Android. r. drawable. ic_menu_edit); menu. add (menu. none, menu. first + 3, 6, "help "). seticon (Android. r. drawable. ic_menu_help); menu. add (menu. none, menu. first + 4, 1, "add "). seticon (Android. r. drawable. ic_menu_add); menu. add (menu. none, menu. first + 5, 4, "Detailed "). seticon (Android. r. drawable. ic_menu_info_details); menu. add (menu. none, menu. first + 6, 3, "send "). seticon (Android. r. drawable. ic_menu_send); // return true will take effect return true;} // The menu item is selected for the event @ overridepublic Boolean onoptionsitemselected (menuitem item) {Switch (item. getitemid () {Case menu. first + 1: Toast. maketext (this, "the delete menu has been clicked", toast. length_long ). show (); break; Case menu. first + 2: Toast. maketext (this, "saved menu clicked", toast. length_long ). show (); break; Case menu. first + 3: Toast. maketext (this, "the Help menu has been clicked", toast. length_long ). show (); break; Case menu. first + 4: Toast. maketext (this, "added menu clicked", toast. length_long ). show (); break; Case menu. first + 5: Toast. maketext (this, "details menu clicked", toast. length_long ). show (); break; Case menu. first + 6: Toast. maketext (this, "the sending menu has been clicked", toast. length_long ). show (); break;} return false;} // The Show (); break;} Option menu is disabled in three cases, the menu button is clicked again, the back button is clicked, or the user selects a menu item @ overridepublic void onoptionsmenuclosed (menu) {toast. maketext (this, "option menu disabled", toast. length_long ). show () ;}// events before the menu is displayed @ overridepublic Boolean onprepareoptionsmenu (menu) {toast. maketext (this, "The onprepareoptionsmenu method will be called before the option menu is displayed. You can use this method to adjust the menu according to the current situation.", toast. length_long ). show (); // if false is returned, this method will consume the user-clicked menu action. The oncreateoptionsmenu method will not be called return true ;}}

Method 2:

Mainactivity. Java

Package COM. example. bluetoothchatv1; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. menu; import android. view. menuinflater; import android. view. menuitem; import android. widget. toast; public class mainactivity extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main);} // when you click menu, the system calls the oncreateoptionsmenu method of the current activity, upload a menu object that implements a menu interface for you to use // @ override // public Boolean oncreateoptionsmenu (menu) {// * Add () the four parameters of the method are: // * 1. group. If no group is set, write menu. none, // * 2, ID, this is very important, Android determines the different menu // * 3, order based on this ID, the menu is determined by the size of this parameter. // * 4. The text is displayed. // * // menu. add (menu. none, menu. first + 1, 5, "delete "). seticon (// android. r. drawable. ic_menu_delete); // The seticon () method is used to set the menu icon. Here, the built-in system icon is used. For more information, see /// android. resources starting with R are provided by the system. The resources we provide ourselves start with R. // menu. add (menu. none, menu. first + 2, 2, "save "). seticon (// android. r. drawable. ic_menu_edit); // menu. add (menu. none, menu. first + 3, 6, "help "). seticon (// android. r. drawable. ic_menu_help); // menu. add (menu. none, menu. first + 4, 1, "add "). seticon (// android. r. drawable. ic_menu_add); // menu. add (menu. none, menu. first + 5, 4, "Detailed "). seticon (// android. r. drawable. ic_menu_info_details); // menu. add (menu. none, menu. first + 6, 3, "send "). seticon (// android. r. drawable. ic_menu_send); // return true will take effect // return true; // The menu item is selected @ overridepublic Boolean onoptionsitemselected (menuitem item) {Switch (item. getitemid () {case R. id. item01: Toast. maketext (this, "the delete menu has been clicked", toast. length_long ). show (); break; case R. id. item02: Toast. maketext (this, "saved menu clicked", toast. length_long ). show (); break; case R. id. item03: Toast. maketext (this, "the Help menu has been clicked", toast. length_long ). show (); break; case R. id. item04: Toast. maketext (this, "added menu clicked", toast. length_long ). show (); break; case R. id. item05: Toast. maketext (this, "details menu clicked", toast. length_long ). show (); break; case R. id. item06: Toast. maketext (this, "the sending menu has been clicked", toast. length_long ). show (); break;} return false;} // The Show (); break;} Option menu is disabled in three cases, the menu button is clicked again, the back button is clicked, or the user selects a menu item @ overridepublic void onoptionsmenuclosed (menu) {toast. maketext (this, "option menu disabled", toast. length_long ). show () ;}// events before the menu is displayed @ overridepublic Boolean onprepareoptionsmenu (menu) {toast. maketext (this, "The onprepareoptionsmenu method will be called before the option menu is displayed. You can use this method to adjust the menu according to the current situation.", toast. length_long ). show (); // if false is returned, this method will consume the user-clicked menu action. The oncreateoptionsmenu method will not be called return true ;} @ override public Boolean oncreateoptionsmenu (menu) {log. I ("effecthchat:", "oncreateoptionsmenu"); // getmenuinflater (). inflate (R. menu. activity_menu, menu); menuinflater Inflater = getmenuinflater (); Inflater. inflate (R. menu. activity_menu, menu); Return true ;}}

Modify the activity_menu.xml File

<Menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <item Android: Id = "@ + ID/item01" Android: icon = "@ Android: drawable/ic_menu_add" Android: title = "add"> </item> <item Android: Id = "@ + ID/item02" Android: icon = "@ Android: drawable/ic_menu_edit" Android: title = "edit"> </item> <item Android: Id = "@ + ID/item03" Android: icon = "@ Android: drawable/ic_menu_save" Android: title = "save"> </item> <item Android: Id = "@ + ID/item04" Android: icon = "@ Android: drawable/ic_menu_info_details" Android: title = "details"> </item> <item Android: Id = "@ + ID/item05" Android: icon = "@ Android: drawable/ic_menu_delete" Android: title = "delete"> </item> <item Android: Id = "@ + ID/item06" Android: icon = "@ Android: drawable/ic_menu_help" Android: title = "help"> </item> </menu>

Reference from: http://android.yaohuiji.com/archives/636

Thank you.

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.