Home>

The menus provided under the android platform can be roughly divided into three categories:option menus, context menus and submenus.

When the activity is running in the foreground,If the user presses the menu key on the phone, a corresponding option menu will pop up at the lower end of the screen.But this function needs developer programming to achieve,If this feature is not implemented when developing the application,Then pressing the phone's meun key while the program is running will not work.

For option menus with icons,Only 6 can be displayed at a time. When there are more than 6 menu options, only the first 5 and one extended menu option will be displayed.Clicking on the extended menu option will pop up the remaining menu items.Icons will not appear in extended menu items,However, radio buttons and check boxes can be displayed.

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 choice 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];//favorite menu item group
  menuitem malemenuitem=null;//male menu items
  @override
  public void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.main);
  }
  /** j
   * Initialize options menu
   * /
  @override
  public boolean oncreateoptionsmenu (menu menu) {
    //Single choice menu option
    submenu gendermenu=menu.addsubmenu (main_group, menu_gender, 0, "gender");
    gendermenu.seticon (r.drawable.gender);
    gendermenu.setheadericon (r.drawable.gender);
    malemenuitem=gendermenu.add (gender_group, menu_gender_male, 0, "");
    malemenuitem.setchecked (true);
    gendermenu.add (gender_group, menu_gender_female, 0, "female");
    //Set the menu item to a radio menu item,Mutually exclusive
    gendermenu.setgroupcheckable (gender_group, true, true);
    //check menu option
    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, "program");
    //Set the menu item as a 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 a shortcut key to the determined menu item
    ok.setalphabeticshortcut ("o");//Set character shortcut
    //ok.setnumericshortcut("1");//Set number shortcut
    //ok.setshortcut("a "," 2 ");//Set two shortcut keys at the same time.
Note:When setting multiple times at the same time, only the last setting will work
    return true;//Remember to return true, otherwise invalid
  }
  /**
   * This event is called when an option is selected in a menu option
   * /
  @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;
  }
  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.length ()-1) + ". \ n";
    } else {
      result=result + ". \ n";
    }
    edittext et=(edittext) meunactivity.this.findviewbyid (r.id.edittext);
    et.append (result);
  }
}

operation result:

  • Previous jQuery implementation of editable table examples (2)
  • Next C # Chinese character to Pinyin example (supports all Chinese characters in the GB2312 character set)