[Android] how to add and listen to menus

Source: Internet
Author: User

Add menu

You can add a menu in the onCreateOptionsMenu or onPrepareOptionsMenu method.

Add code:
    menu.add((int groupId, int itemId, int order, charsequence title) .setIcon(drawable ID)
The four parameters of the add () method are:

1. group. If no group is set, write Menu. NONE,

2. Id. This is very important. Android determines different menus based on this Id.

3. Order. Which menu item is prefixed is determined by the size of this parameter

4. Text: display text of menu items

The add () method returns the MenuItem object, calls its setIcon () method, and sets the Icon for the corresponding MenuItem

Example:

@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// TODO Auto-generated method stubmenu. add (0, 1, 1, "set "). setIcon (R. drawable. tab5cog); menu. add (0, 2, 1, "about "). setIcon (R. drawable. tab2users); menu. add (0, 3, 1, "comment "). setIcon (R. drawable. tab3user); menu. add (0, 4, 1, "quit "). setIcon (R. drawable. tab4wrench); return super. onCreateOptionsMenu (menu );}

Add layout files:
    getMenuInflater().inflate(R.menu.options_menu, menu);
Call getMenuInflater () of the Activity to obtain a MenuInflater,

Use the inflate method to load the menu defined in the layout file to the menu object corresponding to the second parameter.

Example:

@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// TODO Auto-generated method stub // call getMenuInflater () of the Activity to get a MenuInflater, // use the inflate method to load the menu defined in the layout file to the menu object getMenuInflater () corresponding to the second parameter (). inflate (R. menu. main_menu, menu); return super. onCreateOptionsMenu (menu );}

Layout file: Create a menu folder under the res directory and create the layout file main_menu.xml.

<? Xml version = "1.0" encoding = "UTF-8"?> <Menu xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: id = "@ + id/menu_setting" android: title = "set" android: icon = "@ drawable/tab5cog"> </item> <item android: id = "@ + id/menu_about" android: title = "about" android: icon = "@ drawable/tab2users"> </item> <item android: id = "@ + id/menu_idea" android: title = "opinion" android: icon = "@ drawable/tab3user"> </item> <item android: id = "@ + id/menu_exit" android: title = "logout" android: icon = "@ drawable/tab4wrench"> </item> </menu>

Menu item listening:

OnOptionsItemSelected (MenuItem item) is triggered as long as the menu item in the menu is clicked)

The item parameter is the clicked menu Item. In this method, you need to determine which item is clicked to implement different operations.

For two different menu adding methods, the method of listening is a little different, but the essence is the same.

How to add a listener to the Code menu

@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// TODO Auto-generated method stubswitch (item. getItemId () {// obtain the itemIdcase 1 of the item to be clicked: // the corresponding ID is the IdToast set in the add method. makeText (MainActivity. this, "set", Toast. LENGTH_LONG ). show (); break; case 2: Toast. makeText (MainActivity. this, "about", Toast. LENGTH_LONG ). show (); break; case 3: Toast. makeText (MainActivity. this, "comment", Toast. LENGTH_LONG ). show (); break; case 4: finish (); break; default: break;} return super. onOptionsItemSelected (item );}

How to add a menu to a layout File

@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// TODO Auto-generated method stubswitch (item. getItemId () {// obtain the itemIdcase R of the item to be clicked. id. menu_setting: // The Id defined in the layout file. id. XXX method to obtain Toast. makeText (MainActivity. this, "set", Toast. LENGTH_LONG ). show (); break; case R. id. menu_about: Toast. makeText (MainActivity. this, "about", Toast. LENGTH_LONG ). show (); break; case R. id. menu_idea: Toast. makeText (MainActivity. this, "comment", Toast. LENGTH_LONG ). show (); break; case R. id. menu_exit: finish (); break; default: break;} return super. onOptionsItemSelected (item );}


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.