Android Menu details-understanding the Menu in android

Source: Internet
Author: User

Understanding the Menu of Android is an indispensable part of many applications. Especially in Android, all mobile phones carrying Android systems even have a "Menu" key, this shows the particularity of menus in Android programs. The Android SDK provides the following menus: option menu: The most common menu, which is called the option menu Sub-menu in android: in android, click the sub-menu to display the sub-menu items in the floating window. Sub-menus do not support nesting. That is, sub-menus cannot include any other sub-menus. Context Menu: the menu that appears after a view control in android. In windows, right-click the menu that is displayed, 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, if 6th menu items are automatically changed to "more", click "more", and other menu items are displayed. Android3.0 introduces another item called action bar. This article will not explain it, but you will google it yourself. 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 items can be used to indicate that 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), so the sub-Menu can also 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. Before creating the options menu, we mentioned that the Android activity has already created the android. view. Menu object for us in advance, and provided the callback method onCreateOptionsMenu (Menu 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 ). @ Overridepublicboolean onCreateOptionsMenu (Menu menu) {// call the parent class method to add the system Menu. // although no System menu exists for android, it is best to add super to ensure compatibility with future versions. onCreateOptionsMenu (menu); // Add a menu item (in multiple ways) // 1. specify the Title menu directly. add ("menu item 1"); // 2. specify the Title menu through the resource. add (R. string. menuitem2); // 3. displays the group number, ID, order number, and Title menu of the specified menu item. add (1, // group number Menu. FIRST, // unique ID Menu. FIRST, // sorting number "menu item 3"); // Title // if you want to display the menu, return truereturntrue ;} the code above demonstrates three ways to add a menu item. The following explains the third method add (int g RoupId, 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 @ Overridepublicboolean onCreateOptionsMenu (menu Menu) {super. onCreateOptionsMenu (menu); // Add four menu items, divided 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 the menu returntrue;} You can group the menu items above, after grouping, you can use the method provided in menu to operate the Group, as shown below: menu. removeGroup (group1); // delete a menu. setGroupVisible (gou Rp2, visible); // sets whether a menu group is visible. setGroupEnabled (gourp2, enabled); // you can specify whether a menu group can be clicked. setGroupCheckable (gourp2, checkable, exclusive); // sets the check conditions of a group of menus. Response menu items android provides multiple response menu items, 1. The most common method used through the onOptionsItemSelected 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 input the clicked menu item. @ Overridepublicboolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {// respond to each menu item (via the menu item ID) case1: // do something herebreak; case2: // do something herebreak; case3: // do something herebreak; case4: // do something herebreak; default: // for events not processed, hand it to the parent class for returnsuper. onOptionsItemSelected (item);} // returns true, indicating that the event of the menu item has been processed and does not need to be propagated to returntrue ;} the above code can be used as a model for responding to the menu using the onOptionsItemSelected Method The menu ID is hardcoded in the program for convenience. You can use constants or resource IDs to make the code more robust. 2. Although the first method to use listeners is recommended, android provides a listener similar to java swing to respond to menus. The listener can be used in two steps: // Step 1: Create the listener class MyMenuItemClickListener implements OnMenuItemClickListener {@ Override publicboolean onMenuItemClick (MenuItem item) {// do something here... returntrue; // finish handling} // Step 2: register the listener menuItem for the menu item. setOnMenuItemClickListener (new MyMenuItemClickListener (); Description of onMenuItemClick (MenuItem item) callback method in android is "Called when a menu item has been invoked. this is the first cod E that is executed; if it returns true, no other callbacks will be executed. "This method is executed before onOptionsItemSelected. 3. In the Intent response menu, the setIntent (Intent 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.

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.