Capture menu button click events in Android

Source: Internet
Author: User

@ Override
Public Boolean oncreateoptionsmenu (menu ){
/*
* The four parameters of the add () method are: 1. group. If no group is set, set menu. None,
* 2. Id. Android determines different menus Based on the ID. 3. The order of the menus is determined by the size of this parameter.
* 4. Text: display text of menus
*/
Menu. Add (menu. None, menu. First + 1, 5, "delete"). seticon (Android. R. drawable. ic_menu_delete );
// The seticon () method sets the menu icon. the built-in icons are used here.
// Resources starting with Android. 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
Return true;

}

// Menu item selected event
@ Override
Public Boolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid () {freight transport expert
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, "the details menu has been 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 option menu is disabled. In three cases, the menu button is clicked again, the back button is clicked, or a menu item is selected.
@ Override
Public void onoptionsmenuclosed (menu ){
Toast. maketext (this, "option menu disabled", Toast. length_long). Show ();
}

// Events before the menu is displayed
@ Override
Women's brand list
Public 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, the oncreateoptionsmenu method will not be called.
Return true;
}

Related Article

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.