Android Options Menu Usage Example Analysis _android

Source: Internet
Author: User

The examples in this article describe the use of the Android Options menu. Share to everyone for your reference. Specifically as follows:

The menus offered under the Android platform can be broadly grouped into three categories: Options menu, Context menu, and submenu.

When the activity is running in the foreground, if the user presses the menu button on the Phone, the option menu pops up at the lower end of the screen. But this feature requires developer programming to implement it, and if you don't implement it when you develop your application, then pressing the phone's Meun key when the program is running won't work.

For the option menu that carries the icon, you can display up to 6 only, and when there are more than 6 menu options, only the first 5 and one extended menu option will appear, and clicking the Extended menu option pops up the rest of the menu items. An icon will not appear in an extended menu item, but radio buttons and check boxes can be displayed.

The following simulation Options menu

Main.xml Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:id=
"@+id/linearlayout01"
  android: Layout_width= "Fill_parent" 
  android:layout_height= "fill_parent"
  android:orientation= "vertical
  " Xmlns:android= "Http://schemas.android.com/apk/res/android" >
  <scrollview android:id= "@+id/scrollview"
    android:layout_width= "fill_parent"
    android:layout_height= "fill_parent" >
    <edittext android:id = "@+id/edittext"
      android:layout_width= "fill_parent"
      android:layout_height= "Fill_parent" 
      Android : editable= "false"
      android:cursorvisible= "false" 
      android:text= "your selection is \ n" >
    </EditText>
  </ScrollView>
</LinearLayout>

Meunactivity class:

Package com.ljq.activity;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.SubMenu;
Import Android.view.MenuItem.OnMenuItemClickListener;
Import Android.widget.EditText; 
  public class Meunactivity extends activity {private final int menu_gender_male = 0;
  Private final int menu_gender_female = 1; 
  Private final int menu_hobby1 = 2;
  Private final int menu_hobby2 = 3; 
  Private final int menu_hobby3 = 4; 
  Private final int menu_ok = 5;
  Private final int menu_gender = 6; 
  Private final int menu_hobby = 7;
  Private final int gender_group = 0; 
  Private final int hobby_group = 1; 
  Private final int main_group = 2; menuitem[] Hoddymenuitems = new menuitem[3];//Hobby menu item group MenuItem Malemenuitem = null;//Male menu item @Override public void on
    Create (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main); /**J * Initialization Options Menu * * @Override PUBlic boolean Oncreateoptionsmenu (Menu menu) {//Radio menu option submenu Gendermenu = Menu.addsubmenu (Main_group, Menu_gende
    R, 0, "gender");
    Gendermenu.seticon (R.drawable.gender);
    Gendermenu.setheadericon (R.drawable.gender);
    Malemenuitem = Gendermenu.add (gender_group, Menu_gender_male, 0, "male");
    Malemenuitem.setchecked (TRUE);
    Gendermenu.add (Gender_group, Menu_gender_female, 0, "female");
    Sets the menu item to be a radio menu item, mutually exclusive Gendermenu.setgroupcheckable (Gender_group, True, true);
    Check menu options Submenu Hobbymenu = Menu.addsubmenu (main_group,menu_hobby,0, "hobby");
    Hobbymenu.seticon (R.drawable.hobby);
    Hobbymenu.setheadericon (R.drawable.hobby);
    Hoddymenuitems[0]=hobbymenu.add (Hobby_group, menu_hobby1, 0, "swimming");
    Hoddymenuitems[1]=hobbymenu.add (Hobby_group, menu_hobby2, 0, "singing");
    Hoddymenuitems[2]=hobbymenu.add (Hobby_group, menu_hobby3, 0, "programming");
    Sets the menu item to the Check menu item hoddymenuitems[0].setcheckable (TRUE);
    Hoddymenuitems[1].setcheckable (TRUE); Hoddymenuitems[2].setchecKable (TRUE);
    Determine the menu item MenuItem ok=menu.add (main_group,menu_ok,0, "OK");
        Ok.setonmenuitemclicklistener (New Onmenuitemclicklistener () {public boolean Onmenuitemclick (MenuItem item) {
        Appendstatestr ();
      return true;
    }
    }); Add shortcut key ok.setalphabeticshortcut (' O ') to the OK menu item;//Set character accelerator//ok.setnumericshortcut (' 1 ');//Set Numeric shortcut key//ok.setshortcut ( ' A ', ' 2 ');//Set two shortcuts at the same time. Note: When set multiple times, only the last setting works return true; Remember to return true, otherwise invalid}/** * Call this event when an option in the menu option is selected * * @Override public boolean onoptionsitemselected (MenuItem item  
       {switch (Item.getitemid ()) {case Menu_gender_male:case MENU_GENDER_FEMALE:item.setChecked (true);
       Appendstatestr ();  
     Break
      Case Menu_hobby1:case Menu_hobby2:case MENU_HOBBY3:item.setChecked (!item.ischecked ());
      Appendstatestr ();  
    Break
  return true;
    The public void Appendstatestr () {String result = "The gender you selected is:"; if (Malemenuitem.iscHecked ()) {result = result + "male";
    else {result = result + "female";
    } String hobbystr = ""; for (MenuItem hoddy:hoddymenuitems) {if (hoddy.ischecked ()) {hobbystr = Hobbystr + hoddy.gettitle () + ",
      "; } if (Hobbystr.length () > 0) {result = result + ", your hobby is:" + hobbystr.substring (0, HOBBYSTR.L Ength ()-1) + ".
    \ n "; else {result = result +.
    \ n ";
    } EditText et = (edittext) MeunActivity.this.findViewById (R.id.edittext);
  Et.append (result); }
}

Run Result:

I hope this article will help you with your Android program.

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.