Android Study Notes (9) ---- three menu usage for Android

Source: Internet
Author: User

/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.
**************************************** **************************************** ************/

1. About menu of Android

There are three types of menus in the Android system: optionsmenu, contextmenu, and submenu.

Android phones generally use a menu key. When you press the menu key, each activity will respond to this event. Menu options are arranged from left to right from top to bottom. A maximum of six options can be displayed. If there are more than six options, the sixth option is "more". Click to view more options.

2. optionsmenu usage

2.1 create menu

When you press the menu key, the menu option is displayed at the bottom of the screen to provide selection. The method for creating optionsmenu is to rewrite oncreateoptionsmenu () and then add various options through the menu. Add () method. The detailed parameters and return values of the add method are as follows.

public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title) 

Groupid the Group Identifier that this item shocould be part of. This can be used to define groups of items for batch state changes.

Normally use none if an item shocould not be in a group.
Itemid unique item ID. use none if you do not need a unique ID.
Order the order for the item. use none if you do not care about the order. See getorder ().
Title the text to display for the item.

Returns the newly added menu item.

First, group, used to divide Itemid. It is generally menu. None.

Second, id. Is the menu identification number for identifying the menu. Important. Third, order. The size of this parameter determines the order in which menu options appear. The Order varies from small to large. Fourth, display text.

If the return value is "Turn", the menu is valid. If the return value is false, the menu cannot be displayed, that is, the menu is invalid.
The rewrite method oncreateoptionsmenu () has a return Super. oncreateoptionsmenu (menu);, which is the default value.
Example of adding the menu option:

Menu. Add (menu. None, 0, 0, "add"). seticon (Android. R. drawable. ic_menu_add );

2.2 process menu
You can rewrite the onoptionsitemselected () method, then filter different itemids through item. getitemid (), and then execute the corresponding method.

Switch (item. getitemid () {Case 0: Toast. maketext (this, "add", toast. length_short ). show (); break; default: Toast. maketext (this, "method not defined", toast. length_short ). show (); break;} return Super. onoptionsitemselected (item );

2.3 other methods
Brief description of onprepareoptionsmenu () method
When you click the menu button, you can use this method to define event processing before the menu is displayed.

Onoptionsmenuclosed ()
After exiting the menu, you can use this method to define the event processing. The menu button is clicked again, the back button is clicked, or a menu option is selected, the menu will exit.

==================================

All demo source code

/* Author: conowen * Date: 2012.2.25 */package conowen. szu. menu; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuitem; import android. widget. toast; public class menuactivity extends activity {/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// todo auto-generated method stubmenu. add (menu. none, 0, 0, "add "). seticon (Android. r. drawable. ic_menu_add); menu. add (menu. none, 1, 1, "save") // set the icon. seticon (Android. r. drawable. ic_menu_save); menu. add (menu. none, 2, 2, "send "). seticon (Android. r. drawable. ic_menu_send); menu. add (menu. none, 3, 3, "details "). seticon (Android. r. drawable. ic_menu_info_details); menu. add (menu. none, 4, 4, "delete "). seticon (Android. r. drawable. ic_menu_delete); menu. add (menu. none, 5, 5, "help "). seticon (Android. r. drawable. ic_menu_help); menu. add (menu. none, 6, 6, "Search "). seticon (Android. r. drawable. ic_menu_search); return Super. oncreateoptionsmenu (menu); // return false ;}@ overridepublic Boolean onoptionsitemselected (menuitem item) {// todo auto-generated method stubswitch (item. getitemid () {Case 0: Toast. maketext (this, "add", toast. length_short ). show (); break; Case 1: Toast. maketext (this, "save", toast. length_short ). show (); break; Case 2: Toast. maketext (this, "send", toast. length_short ). show (); break; Case 3: Toast. maketext (this, "Detailed", toast. length_short ). show (); break; default: Toast. maketext (this, "method not defined", toast. length_short ). show (); break;} return Super. onoptionsitemselected (item) ;}@ overridepublic Boolean onprepareoptionsmenu (menu) {// todo auto-generated method stubtoast. maketext (this, "you can define the processing method when menu is not displayed", toast. length_short ). show (); return Super. onprepareoptionsmenu (menu) ;}@ overridepublic void onoptionsmenuclosed (menu) {// todo auto-generated method stubtoast. maketext (this, "After menu exits, define the processing method by yourself", toast. length_short ). show (); super. onoptionsmenuclosed (menu );}}

3. submenu usage

Submenu implements the sub-menu function. For example, the next menu of "add" is "add from network" and "add two options from local menu"

Submenu = menu. addsubmenu (menu. none, 0, 0, "add "). seticon (Android. r. drawable. ic_menu_add); submenu. add (menu. none, 10, 0, "add from network"); submenu. add (menu. none, 11, 1, "add from local ");

The submenu. Add () method does not support setting icons. It is also invalid.

/* Author: conowen * Date: 2012.2.25 */package conowen. szu. menu; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuitem; import android. view. submenu; import android. widget. toast; public class menuactivity extends activity {/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// todo auto-generated method stubsubmenu submenu = menu. addsubmenu (menu. none, 0, 0, "add "). seticon (Android. r. drawable. ic_menu_add); submenu. add (menu. none, 10, 0, "add from network"); submenu. add (menu. none, 11, 1, "add from local"); menu. add (menu. none, 1, 1, "save") // set the icon. seticon (Android. r. drawable. ic_menu_save); return Super. oncreateoptionsmenu (menu); // return false ;}@ overridepublic Boolean onoptionsitemselected (menuitem item) {// todo auto-generated method stubswitch (item. getitemid () {Case 0: Toast. maketext (this, "add", toast. length_short ). show (); break; Case 1: Toast. maketext (this, "save", toast. length_short ). show (); break; case 10: Toast. maketext (this, "add from network", toast. length_short ). show (); break; Case 11: Toast. maketext (this, "add from local", toast. length_short ). show (); break; default: break;} return Super. onoptionsitemselected (item );}}

4. contextmenu usage

After a view is pressed for 2 seconds, the menu is displayed. The created method is the oncreatecontextmenu () method, which is oncontextitemselected (). Because it is a long-pressed view, contextmenu must be registered to the target view to achieve this.

The registration method is as follows (for example, if you register an edittext file, you can call up the menu by long pressing the edittext file). Et is the ID of edittext.

this.registerForContextMenu(findViewById(R.id.et));

The add () method in oncreatecontextmenu cannot add the icon () seticon. Even if it is added, it cannot be displayed. You can also set the submenu in the oncreatecontextmenu method. For details about the method, refer to 3rd.

/* Author: conowen * Date: 2012.2.25 */package conowen. szu. menu; import android. app. activity; import android. OS. bundle; import android. view. contextmenu; import android. view. contextmenu. contextmenuinfo; import android. view. menu; import android. view. menuitem; import android. view. view; import android. widget. toast; public class menuactivity extends activity {/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); this. registerforcontextmenu (findviewbyid (R. id. et); // register it to edittext, and ET is its ID} @ overridepublic void oncreatecontextmenu (contextmenu menu, view V, contextmenuinfo menuinfo) {// todo auto-generated method stubmenu. add (menu. none, 0, 0, "add"); menu. add (menu. none, 1, 1, "save"); menu. add (menu. none, 2, 2, "send"); menu. add (menu. none, 3, 3, "Detailed"); menu. add (menu. none, 4, 4, "delete"); menu. add (menu. none, 5, 5, "help"); menu. add (menu. none, 6, 6, "Search"); super. oncreatecontextmenu (menu, V, menuinfo) ;}@ overridepublic Boolean oncontextitemselected (menuitem item) {// todo auto-generated method stubswitch (item. getitemid () {Case 0: Toast. maketext (this, "add", toast. length_short ). show (); break; Case 1: Toast. maketext (this, "save", toast. length_short ). show (); break; Case 2: Toast. maketext (this, "send", toast. length_short ). show (); break; Case 3: Toast. maketext (this, "Detailed", toast. length_short ). show (); break; default: Toast. maketext (this, "method not defined", toast. length_short ). show (); break;} return Super. oncontextitemselected (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.