Personal Android learning-activity activation menu

Source: Internet
Author: User

Take a break on Saturday. If you don't have any learning skills, you can continue to work hard and cheer up (* ^__ ^ *)!

To learn about the menu, we need to know what a menu is. Let's take a look ~

Open the simulator or mobile phone, click the menu key, and the flag location is the menu

The options menu of Android is invisible by default. When you press the menu key, the program menu will appear below the screen. The Android system has a maximum of six menu units by default. If there are more than six menus, the system displays "more" in the 6th menu units ".

Android menus must be reflected through four interfaces.

The picture is not good. What do you mean ~

The menu interface is a parent interface with two subinterfaces.

Submenu: it represents a sub-menu. It can contain multiple menuitems to form a menu item ).

Contextmenu: A context menu. It can also contain multiple menuitems.

Menu defines the following methods to add menus and menu items.

Menuitem add (INT titleres) to add a new menu item.

Menuitem add (INT groupid, int Itemid, int order, charsequence title) Add a new menu item in the groupid Group

Menuitem add (INT groupid, int Itemid, int order, int titleres) Add a new menu item in the groupid Group

Menuitem add (charsequence title) Add a new menu item

Submenu addsubmenu (INT titleres) to add a new sub menu

Submenu addsubmenu (INT groupid, int Itemid, int order, int titleres) Add a new sub menu in the groupid Group

Summary: The add method is used to add menu items. The addsubmenu method is used to add sub-menus. The differences between these methods are: whether to add sub-menus and menu items to the specified menu, whether to use string Resources in the resource file to set the title.

Next, let's take a look at how to add menus and sub-menus in the android program.

To achieve this effect, add an edittext in the activity. I can change the font color and size through the menu and sub-menu below.

Well, now we can see that to achieve this effect, we need to add three menu items. In the font menu, we need to add five sub menu items, in the color menu item, we need to add three sub menu items. Next we start to write the code.

The first step we need to do: override the oncreateoptionsmenu (menu) method in the activity, and call the menu object method in this method to add menu items or sub menus

Step 2: If you want the program to respond to the Click Event of the menu item, rewrite the onoptionsitemselected (menuitem mi) method of the activity.

The layout file of this program only adds an edittext in the activity, which is not displayed here.

Java code:

Package song. activity; import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. view. menu; import android. view. menuitem; import android. view. submenu; import android. widget. edittext; import android. widget. toast; public class menutestactivity extends activity {// The identifier of the menu item defining the font size. Final int font_10 = 0x111; Final int font_12 = 0x112; final int font_14 = 0x113; Final int font _ 16 = 0x114; Final int font_18 = 0x115; // define the final int plain_item = 0x11b of a normal menu item; // define the identifier of the font color menu item final int font_red = 0x116; Final int font_blue = 0x117; Final int font_green = 0x118; private edittext edit; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Edit = (edittext) findviewbyid(r.id.txt) ;}@ overridepublic Boolean oncreateopti Onsmenu (menu) {// ------------- Add a font size sub menu to menu ----------- submenu fontmenu = menu. addsubmenu ("font size"); // set the fontmenu icon of the menu. seticon (R. drawable. font); // set the fontmenu icon in the menu header. setheadericon (R. drawable. font); // set the title fontmenu of the menu header. setheadertitle ("select font size"); fontmenu. add (0, font_10, 0, "font 10"); fontmenu. add (0, font_12, 0, "12 font"); fontmenu. add (0, font_14, 0, "font 14"); fontmenu. add (0, font_16, 0, "font 16"); fontm Enu. add (0, font_18, 0, "18 font"); // ------------- Add a normal menu item to menu ------------- menu. add (0, plain_item, 0, "normal menu item"); // ------------- Add a text color submenu to menu ------------- submenu colormenu = menu. addsubmenu ("font color"); colormenu. seticon (R. drawable. color); // set the colormenu icon in the menu header. setheadericon (R. drawable. color); // set the title colormenu of the menu header. setheadertitle ("select text color"); colormenu. add (0, font_red, 0, "Red"); colormenu. add (0, font_green, 0, "Green"); colormenu. add (0, font_blue, 0, "blue"); return Super. oncreateoptionsmenu (menu) ;}@ override // callback method after the menu item is clicked public Boolean onoptionsitemselected (menuitem mi) {// you can determine which menu item to click, and make targeted responses. Switch (MI. getitemid () {Case font_10: Edit. settextsize (10*2); break; Case font_12: Edit. settextsize (12*2); break; Case font_14: Edit. settextsize (14*2); break; Case font_16: Edit. settextsize (16*2); break; Case font_18: Edit. settextsize (18*2); break; Case font_red: Edit. settextcolor (color. red); break; Case font_green: Edit. settextcolor (color. green); break; Case font_blue: Edit. settextcolor (color. blue); break; Case plain_item: Toast = toast. maketext (menutestactivity. this, "you have clicked the normal menu item", toast. length_short); toast. show (); break;} return true ;}}

As you can see, all my code is completed in Java, how can I use XML to define the option menu?

Android. View. menuinflater

This class is used to instantiate menu XML files into menu objects. This class is used to instantiate the menu XML file as a menu object

First, create a menu folder and create an option_menu.xml file. The file content is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <item Android: Id = "@ + ID/font" Android: icon = "@ drawable/font" Android: title = "Set Font size" Android: titlecondensed = "font size"> <menu> <item Android: Id = "@ + ID/font_10" Android: title = "font 10"/> <item Android: Id = "@ + ID/font_12" Android: Title = "font 12"/> <item Android: id = "@ + ID/font_14" Android: Title = "14 font"/> <item Android: Id = "@ + ID/font_16" Android: title = "16 font"/> <item Android: Id = "@ + ID/font_18" Android: title = "18 font"/> </menu> </item> <item Android: Id = "@ + ID/normal" Android: title = "normal menu item"/> <item Android: Id = "@ + ID/color" Android: icon = "@ drawable/color" Android: title = "font color"> <menu> <item Android: Id = "@ + ID/font_red" Android: Title = "red"/> <item Android: id = "@ + ID/font_green" Android: Title = "yellow"/> <item Android: Id = "@ + ID/font_blue" Android: title = "green"/> </menu> </item> </menu>

Java file of activity:

Package song. activity; import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. view. menu; import android. view. menuinflater; import android. view. menuitem; import android. view. menuitem. onmenuitemclicklistener; import android. view. submenu; import android. widget. edittext; import android. widget. toast; public class menutestactivity extends activity implements onmenuitemc Licklistener {private edittext edit; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Edit = (edittext) findviewbyid(r.id.txt) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {menuinflater Inflater = This. getmenuinflater (); Inflater. inflate (R. menu. option_menu, menu); For (INT I = 0; I <menu. size (); I ++) {menu. getitem (I ). seto Nmenuitemclicklistener (this);} submenu smenu1 = menu. finditem (R. id. font ). getsubmenu (); For (INT I = 0; I <smenu1.size (); I ++) {smenu1.getitem (I ). setonmenuitemclicklistener (this);} submenu smenu2 = menu. finditem (R. id. color ). getsubmenu (); For (INT I = 0; I <smenu2.size (); I ++) {smenu2.getitem (I ). setonmenuitemclicklistener (this);} return true ;}@ overridepublic Boolean onmenuitemclick (menuitem mi) {// you can check whether Which menu item, and make a targeted response. Switch (MI. getitemid () {case R. id. font_10: Edit. settextsize (10*2); break; case R. id. font_12: Edit. settextsize (12*2); break; case R. id. font_14: Edit. settextsize (14*2); break; case R. id. font_16: Edit. settextsize (16*2); break; case R. id. font_18: Edit. settextsize (18*2); break; case R. id. font_red: Edit. settextcolor (color. red); break; case R. id. font_green: Edit. settextcolor (color. green); break; case R. id. font_blue: Edit. settextcolor (color. blue); break; case R. id. normal: Toast = toast. maketext (menutestactivity. this, "you have clicked the normal menu item", toast. length_short); toast. show (); break;} return true ;}}

Where

public boolean onCreateOptionsMenu(Menu menu){MenuInflater inflater = this.getMenuInflater();    inflater.inflate(R.menu.option_menu, menu);        for(int i=0; i<menu.size(); i++){    menu.getItem(i).setOnMenuItemClickListener(this);    }        SubMenu smenu1 = menu.findItem(R.id.font).getSubMenu();        for(int i=0; i<smenu1.size(); i++){    smenu1.getItem(i).setOnMenuItemClickListener(this);    }    SubMenu smenu2 = menu.findItem(R.id.color).getSubMenu();        for(int i=0; i<smenu2.size(); i++){    smenu2.getItem(i).setOnMenuItemClickListener(this);    }        return true;}

Code for converting XML files


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.