Android menu Summary

Source: Internet
Author: User

In Android, menus are divided into three types: optionsmenu, contextmenu, and submenu ).

1. optionsmenu

Common methods to be overwritten in an activity:

Public Boolean oncreateoptionsmenu (menu): use this method to create optionsmenu. This method will only be executed when the option menu is displayed for the first time.

Public Boolean onoptionsitemselected (menuitem item): The action that occurs after the menu item is selected.

Public void onoptionsmenuclosed (menu): The action that occurs after the menu is closed.

Public Boolean onprepareoptionsmenu (menu): The onprepareoptionsmenu method is called before the option menu is displayed. You can use this method to adjust the menu according to the current situation.

Public Boolean onmenuopened (INT featureid, menu): The action that occurs after a single open.

 

1) create a 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
Super. Oncreateoptionsmenu (menu );

// Three methodsAdd menu item
 //  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, so that the Group can quickly operate the same group of menus (such as menu. removegroup (group1 ))
Menu. First, // The unique ID number can be specified by yourself or automatically assigned by the system. In response to the menu, you need to use the ID number to determine which menu has been clicked.
Menu. First, // The sorting number. If the number is small, it is displayed in front.
"Menu item 3 "); // Title

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

2) response menu items

@ 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 :
// The parent class is responsible for handling events that have not been processed.

Returnsuper. onoptionsitemselected (item );

}

// If true is returned, the event of the menu item is processed and does not need to be propagated.
Return True ;
}

You can also create a listener to respond to menu events, which is executed before onoptionsitemselected.

//Step 1: Create a listener class
ClassMymenuitemclicklistenerImplementsOnmenuitemclicklistener {

@ Override
Publicboolean onmenuitemclick (menuitem item ){
//Do something here...
Return true;//Finish handling
}
}

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

2. Context menu, option menu, and sub-menu

A complete case

Res/layout/Main. xml

 <?  XML version = "1.0" encoding = "UTF-8"  ?> 
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: Orientation = "Vertical" Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent" >

< Textview Android: ID = "@ + ID/txt1" Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content" Android: Text = "@ String/hello_contextmenu" />

< Textview Android: ID = "@ + ID/txt2" Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content" Android: Text = "@ String/hello_submenu" />

</ Linearlayout >

Res/values/strings. xml

 <? XML version = "1.0" encoding = "UTF-8"  ?> 
< Resources >
< String Name = "Hello_contextmenu" > Hello context menu </ String >
< String Name = "Hello_submenu" > Hello context sub menu </ String >
< String Name = "App_name" > Webabcd_menu </ String >
</ Resources >

Main. Java

 Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. contextmenu;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. View. submenu;
Import Android. View. view;
Import Android. View. contextmenu. contextmenuinfo;
Import Android. widget. textview;
Import Android. widget. Toast;

// The following shows how to implement two menus: context menu (call out the menu by long-clicking an element) and option menu (call out the menu by pressing the menu button on the mobile phone)
Public Class Main Extends Activity {
/** Called when the activity is first created. */
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

// Register a context menu for r.id.txt 1 (long press on this textview, the context menu will be called out)
// The oncreatecontextmenu must be rewritten to create the specific outbound menu content.
Textview txt1 = (textview) This .Findviewbyid(r.id.txt 1 );
This . Registerforcontextmenu (txt1 );

// Register a context menu for r.id.txt 2
Textview txt2 = (textview) This .Findviewbyid(r.id.txt 2 );
This . Registerforcontextmenu (txt2 );
}

// Override oncreatecontextmenu to create context menu
// Override oncontextitemselected to respond to context menu
@ Override
Public Void Oncreatecontextmenu (contextmenu menu, view V,
Contextmenuinfo menuinfo ){
Super . Oncreatecontextmenu (menu, V, menuinfo );

// Create context menu for r.id.txt 1
If (V = (textview) This .Findviewbyid(r.id.txt 1 )){

// Contextmenu. seticon ()-set the menu icon
// Contextmenu. setheadertitle ()-set the menu title
Menu. setheadericon (R. drawable. icon01 );
Menu. setheadertitle ("I Am a menu ");

// Use contextmenu. Add () to add a menu item. The returned value is menuitem.
// First parameter: Group ID
// Second parameter: menu item ID
// Third parameter: Sequence Number
// Fourth parameter: content displayed on the menu item
Menu. Add (1, 0, 0, "menu 1 ");

// Menuitem-return type after a menu item is added. Other menu items are set to operate on this object.
Menu. Add (1, 1, 1, "menu 2"). setcheckable ( True );

}
// Create a context menu for r.id.txt 2 (multi-level context menu)
Else If (V = (textview) This .Findviewbyid(r.id.txt 2 )){

// Contextmenu. addsubmenu ("menu name")-used to add a submenu. A sub-menu is actually a special menu
Submenu sub = menu. addsubmenu ("parent menu 1 ");
Sub. seticon (R. drawable. icon01 );
Sub. Add (0, 0, 0, "menu 1 ");
Sub. Add (0, 1, 1, "menu 2 ");
Sub. setgroupcheckable (1, True , True );

Submenu sub2 = menu. addsubmenu ("parent menu 2 ");
Sub2.seticon (R. drawable. icon01 );
Sub2.add (1, 0, 0, "menu 3 ");
Sub2.add (1, 1, 1, "menu 4 ");
Sub2.setgroupcheckable (1, True , False );

}
}


// Override oncreateoptionsmenu to create option menu
@ Override
Public Boolean Oncreateoptionsmenu (menu ){

Menuitem = menu. Add (0, 0, 0, "menu 111111111111111111111 ");

// Menuitem. seticon ()-set the icon of the menu item
// Menuitem. settitlecondensed ()-the title of the menu. If you specify a title, the title on the menu item will be subject to this title.
// Menuitem. setalphabeticshortcut ()-set the shortcut key for selecting a menu item
// Note: if there are more than 6 menu items, the 6th menu will be changed to the more menu, and the extra menu will be displayed after you click the more menu.
Menuitem. seticon (R. drawable. icon01 );
Menuitem. settitlecondensed ("menu 1 ");
Menuitem. setalphabeticshortcut ('A ');

Menu. Add (0, 1, 1, "menu 2"). seticon (R. drawable. icon02 );
Menu. Add (0, 2, 2, "menu 3"). seticon (R. drawable. icon03 );
Menu. Add (0, 3, 3, "menu 4 ");
Menu. Add (0, 4, 4, "menu 5 ");
Menu. Add (0, 5, 5, "menu 6 ");
Menu. Add (0, 6, 6, "menu 7"). seticon (R. drawable. icon04 );
Menu. Add (0, 7, 7, "menu 8"). seticon (R. drawable. icon05 );

Return True ;
}

// Override onoptionsitemselected to respond to the option menu
@ Override
Public Boolean Onoptionsitemselected (menuitem item ){
Super . Onoptionsitemselected (item );

Toast. maketext (main. This , "The menu item to be clicked is:" + String. valueof (item. getitemid (), Toast. length_short). Show ();

Return False ;
}
}

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.