Android menu. Add ()

Source: Internet
Author: User

The use of menus is indispensable for any software. In Android, each activity is bound with a menu. To define and use a menu, you must perform operations under the activity and rewrite the oncreateoptionsmenu and onoptionsitemselected methods. Example:

@ Override

Public Boolean oncreateoptionsmenu (menu ){

// Use the base class method to bring up the system menu (if any)

Super. oncreateoptionsmenu (menu );

Menu. Add (0, 1, 0, "Start again"). seticon (R. drawable. reflash );

Menu. Add (0, 2, 0, "game guide"). seticon (R. drawable. Help );

Menu. Add (0, 3, 0, "about games"). seticon (r.drawable.info );

Menu. Add (, 0, "don't want to play"). seticon (R. drawable. Exit );

// The return value is "true", indicating that the menu is visible, that is, the menu is displayed.

Return true;

}

Take a look at the parameters of the menu. Add method:

The first int type group ID parameter represents the group concept. You can group several menu items to better manage your menu buttons in a group.

The item ID parameter of the second int type represents the project number. This parameter is very important. An item ID corresponds to an option in a menu. This item ID is used to determine which option you are using when using the menu later.

The third 'order id' parameter of 'int' type represents the order in which menu items are displayed. The default value is 0, indicating that the display order of the menu is displayed according to the display order of ADD.

The fourth string type title parameter, indicating the text displayed in the option.

Let's take a look at the seticon method, which is to add the icon display for the menu and use the resource source file.

After the menu is designed, continue to rewrite the onoptionsitemselected method to respond to the menu. Sample Code:

@ Override

Public Boolean onoptionsitemselected (menuitem item ){

Super. onoptionsitemselecte (item );

Switch (item. getitemid ()){

Case1:

// Process the code

Case2:

// Process the code

Case: 3

// Process the code

Case: 4

Finish ();

Break;

}

Return true;

}

 

Http://dev.10086.cn/cmdn/supesite? 2515124/viewspace-36169

 

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.