Android menu details (2) -- create and respond to option menus

Source: Internet
Author: User

In the previous article, "android menu details (1) -- Understanding menu in Android" briefly introduces the menu of Android, today, let's take a look at how to create and respond to the most common options menu through code ).

Create Options Menu

As mentioned above, the android activity has already created the Android. View. Menu object for us in advance, and provided the callback method oncreateoptionsmenu (menu) for us to initialize the menu content. This method will only be executed when the option menu is displayed for the first time.Dynamically change the option menu content, Please useonPrepareOptionsMenu(Menu)

 

@ Override
Publicboolean oncreateoptionsmenu (menu ){
// Call the parent class method to add the System Menu
// Although there is no system menu for Android, it is best to add it to later versions to ensure compatibility.
Super. oncreateoptionsmenu (menu );

// Add menu items (multiple methods)
// 1. Specify the title directly
Menu. Add ("menu item 1 ");
// 2. Specify the title through the resource
Menu. Add (R. String. menuitem2 );
// 3. display the group number, ID, sorting number, and title of the specified menu item
Menu. Add (
1, // group number
Menu. First, // unique ID
Menu. First, // sorting number
"Menu item 3"); // Title

// Return true if you want to display the menu
Returntrue;
}


The code above demonstrates three methods for adding menu items. The third method is explained below.
Add (INT groupid, int Itemid, int order, charsequence title). The first parameter is the group number. In Android, You can group menus to quickly operate the menus in the same group. The second parameter specifies the unique ID of each menu item. You can specify it by yourself or let the system automatically allocate it. In response to the menu, you need to use the ID number to determine which menu is clicked. Therefore, the general practice is to define some ID constants, but there is a better way in Android, that is, to reference them through resource files. This will be introduced later. The third parameter indicates the number of the order in which the menu item is displayed.

Group menu items
@ Override
Publicboolean oncreateoptionsmenu (menu ){
Super. oncreateoptionsmenu (menu );
// Add four menu items into two groups
Int group1 = 1;
Int gourp2 = 2;
Menu. Add (group1, 1, 1, "item 1 ");
Menu. Add (group1, 2, 2, "item 2 ");
Menu. Add (gourp2, 3, 3, "item 3 ");
Menu. Add (gourp2, 4, 4, "item 4 ");
// Display menu
Returntrue;
}

 

You can group the menu items in the upper area. After grouping, you can use the methods provided in menu to operate the Group, as shown below:

 

Menu. removegroup (group1); // delete a group of menus
Menu. setgroupvisible (gourp2, visible); // you can specify whether a set of menus are visible.
Menu. setgroupenabled (gourp2, enabled); // you can specify whether a group of menus can be clicked.
Menu. setgroupcheckable (gourp2, checkable, exclusive); // you can check a group of menus.

 

Response menu item

Android provides a variety of response menu items.

1. onoptionsitemselected Method

The most commonly used method is to override the activity classonOptionsItemSelected(MenuItem)Callback method. Every time a single menu item is clicked, Android calls this method and passes in the clicked menu item.

 

@ Override
Publicboolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid ()){
// Respond to each menu item (by the menu item ID)
Case1:
// Do something here
Break;
Case2:
// Do something here
Break;
Case3:
// Do something here
Break;
Case4:
// Do something here
Break;
Default:
// Submit the event to the parent class for processing.
Returnsuper. onoptionsitemselected (item );
}
// If the returned value is true, the event of the menu item is processed and does not need to be propagated.
Returntrue;
}

 

The above code can be usedResponse menu Template Using onoptionsitemselected MethodFor convenience, the menu ID is hardcoded in the program. You can use constants or resource IDs to make the code more robust.

2. Use listeners

Although the first method is recommended, Android provides a listener similar to Java swing to respond to the menu. The listener can be used in two steps:

 

// Step 1: Create a listener class
Class mymenuitemclicklistener implements onmenuitemclicklistener {
@ Override
Publicboolean onmenuitemclick (menuitem item ){
// Do something here...
Returntrue; // finish handling
}
}

// Step 2: register the listener for the menu item
Menuitem. setonmenuitemclicklistener (New mymenuitemclicklistener ());

 

The android documentation describes the onmenuitemclick (menuitem item) callback method as "called when a menu item has been invoked. this is the first code that is executed; if it returns true, no other callbacks will be executed. "visibleThis method is priorOnoptionsitemselected execution.

3. Use the intent response menu

The setintent (intent) method is called directly on menuitem, so android will automatically call the setintent (intent) method when the menu is clicked.startActivity(Intent). However, I personally think it is better to manually call it in the onoptionsitemselected case.startActivity(Intent).

Conclusion

This article describes in detail how to create and respond to the option menu. Next, "android menu details (iii) -- submenu and iconmenu" will continue to introduce the use of submenus and Icon menus.

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.