Android menu comprehension and response

Source: Internet
Author: User
Tags unique id

Understand Android menus
Menus are an indispensable part of many apps, especially in Android. All mobile phones carrying Android systems even have a "menu" key. This shows the special nature of menus in Android apps. The android SDK provides the following menus:

Option menu: The most common menu. It is called option menu in Android.
Sub-menu: Click sub-menu in Android to display sub-menu items in the floating window. Sub-menus do not support nesting. That is, sub-menus cannot contain any other sub-menus.
Context Menu: the menu that appears after the View Control in Android. In Windows, right-click the menu that appears, that is, the context menu.
Icon menu: this is a simple menu item with an icon. Note that icons cannot be displayed for sub-menu items, context menu items, and extended menu items.
Select menu (alternative menu ~~)
Extended menu: up to six menu items can be displayed in the option menu. If more than six menu items are displayed, the system replaces 6th menu items with a sub menu named "more, all menu items that cannot be displayed are used as sub-menu items of the "more" menu. For example:

 

The android. View. menu interface represents a menu. Android uses it to manage various menu items. Note that we generally do not create a menu by ourselves, because each activity comes with one by default. What we need to do is add a menu item for it and respond to the Click Event of the menu item. Android. View. menuitem indicates each menu item, and Android. View. submenu indicates the sub menu. The relationship between the three can be used to represent

 

As mentioned above, each activity contains one menu, and one menu can contain multiple menu items and multiple sub-menus. The sub-menu is actually a menu (because it implements the menu interface ), therefore, a sub-menu can contain multiple menu items. Submenu inherits the addsubmenu () method of menu, but a runtime error is thrown during the call. Oncreateoptionsmenu () and onoptionsmenuselected () are two callback methods provided in the activity for creating menu items and clicking the response menu items.

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. If you need to dynamically change the content of the option menu, use onprepareoptionsmenu (menu ).

Java code: @ override
 
Public Boolean 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
 
Return true;
 
}
Copy code
The code above demonstrates three methods for adding menu items. The following describes the third method 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

Java code: @ override
 
Public Boolean 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
 
Return true;
 
}
Copy code
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:

Java code:
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.
Copy code
Response menu item
Android provides a variety of response menu items.
1. onoptionsitemselected Method

The most commonly used method is to override the onoptionsitemselected (menuitem) callback method of the activity class. Android calls this method whenever a single menu item is clicked and passes in the clicked menu item.

Java code:
@ Override
 
Public Boolean onoptionsitemselected (menuitem item ){
 
Switch (item. getitemid ()){
 
// Respond to each menu item (by the menu item ID)
 
Case 1:
 
// Do something here
 
Break;
 
Case 2:
 
// Do something here
 
Break;
 
Case 3:
 
// Do something here
 
Break;
 
Case 4:
 
// Do something here
 
Break;
 
Default:
 
// Submit the event to the parent class for processing.
 
Return super. onoptionsitemselected (item );
 
}
 
// If the returned value is true, the event of the menu item is processed and does not need to be propagated.
 
Return true;
 
}
Copy code
The above code can be used as a template for responding to the menu using the onoptionsitemselected method. For 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:

Java code:
// Step 1: Create a listener class
 
Class mymenuitemclicklistener implements onmenuitemclicklistener {
 
@ Override
 
Public Boolean onmenuitemclick (menuitem item ){
 
// Do something here...
 
Return true; // finish handling
 
}
 
}
 

// Step 2: register the listener for the menu item
 
Menuitem. setonmenuitemclicklistener (New mymenuitemclicklistener ());
Copy code
3. In the intent response menu, the setintent (intent) method is called directly on menuitem. In this way, Android automatically calls startactivity (intent) when the menu is clicked ). However, I personally think it is better to manually call startactivity (intent) directly in the onoptionsitemselected case.
The article from: Android mobile phone network (www.anzhuopp.com) Detailed reference: http://www.anzhuopp.com/Androidshouji/2855/

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.