Android平台下提供的菜单大体上分为三类:选项菜单:Option Menu 上下文菜单:Context Menu 和子菜单:Submenu
当Activity 在前台运行时,按下Menu键,此时就会在屏幕底端弹出相应的选项菜单
对于携带图标的选项菜单,每次最多只能显示6个,多余6个时,将只显示5个和一个扩展菜单选项,扩展菜单不显示图标,会显示单选按钮以及复选框
选项菜单相关的回调方法以及说明
onCreateOptionMenu(Menu menu)初始化菜单选项,只在第一次显示菜单时调用,如果每次要更新菜单选项,则需要重写 onPrepareOptionMenu(Menu menu)方法
public boolean onOptionItemSelected(MenuItem item)选项菜单中某个选项被选中时调用
public void onOptionMenuClosed(Menu menu)当选项菜单关闭时(用户按了返回键或者选择了某个菜单项)调用该方法
public boolean onPrepareOptionMenu(Menu menu)为程序准备菜单,每次菜单显示前会调用该方法,重写该方法需要返回true,否则选项菜单将不会显示
Menu类
abstract MenuItem |
add(
CharSequence title)
Add a new item to the menu.
|
abstract MenuItem |
add(int groupId, int itemId, int order, int titleRes)
Variation on
add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself.
|
abstract MenuItem |
add(int titleRes)
Add a new item to the menu.
|
abstract MenuItem |
add(int groupId, int itemId, int order,
CharSequence title)
Add a new item to the menu.
|
向Menu添加一个菜单项,返回一个MenuItem对象
参数说明:
groupId:菜单项所在组的Id,通过分组可以对菜单项批量操作,不属于任何组就传入NONE
itemId:唯一标识菜单项的id,可以传入NONE
order:菜单项的顺序,可以传入NONE
title:菜单项显示的文本内容
titleRes:String对象资源标识符
abstract SubMenu |
addSubMenu(int groupId, int itemId, int order,
CharSequence title)
Add a new sub-menu to the menu.
|
abstract SubMenu |
addSubMenu(int groupId, int itemId, int order, int titleRes)
Variation on
addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself.
|
abstract SubMenu |
addSubMenu(
CharSequence title)
Add a new sub-menu to the menu.
|
abstract SubMenu |
addSubMenu(int titleRes)
Add a new sub-menu to the menu.
|
参数说明:
groupId:菜单项所在组的Id,通过分组可以对菜单项批量操作,不属于任何组就传入NONE
itemId:唯一标识菜单项的id,可以传入NONE
order:菜单项的顺序,可以传入NONE
title:菜单项显示的文本内容
titleRes:String对象资源标识符
abstract void |
clear()
Remove all existing items from the menu, leaving it empty as if it had just been created.移除所有菜单子项
|
abstract void |
close()
Closes the menu, if open.如果菜单显示,关闭菜单
|
abstract MenuItem |
findItem(int id)
Return the menu item with a particular identifier.返回指定id的MenuItem对象
|
MenuItem类
abstract MenuItem |
setAlphabeticShortcut(char alphaChar)字母快捷键
Change the alphabetic shortcut associated with this item设置
字母快捷键
|
abstract MenuItem |
setNumericShortcut(char numericChar)数字快捷键
Change the numeric shortcut associated with this item.设置
数字快捷键
|
abstract MenuItem |
setIcon(
Drawable icon)图标Drawable对象
Change the icon associated with this item.设置图标
|
abstract MenuItem |
setIcon(int iconRes)图标资源id
Change the icon associated with this item.设置图标
|
abstract MenuItem |
setIntent(
Intent intent)与MenuItem绑定的Intent对象
Change the Intent associated with this item.为MenuItem绑定Intent对象,当被选中时将会调用startActivity方法处理相应的Intent
|
abstract MenuItem |
setShortcut(char numericChar, char alphaChar)
Change both the numeric and alphabetic shortcut associated with this item.
|
abstract MenuItem |
setTitle(
CharSequence title)标题名称
Change the title associated with this item.
|
abstract MenuItem |
setTitle(int title)标题资源id
Change the title associated with this item.
|
abstract MenuItem |
setTitleCondensed(
CharSequence title)MenuItem的缩略标题,当不能显示全部标题时显示缩略标题
Change the condensed title associated with this item.
|
abstract MenuItem |
setOnMenuItemClickListener(
MenuItem.OnMenuItemClickListener menuItemClickListener)
Set a custom listener for invocation of this menu item.为MenuItem设置自定义监听器,一般情况使用回调方法onOptionItemSelected会更有效率
|
SubMenu类
abstract SubMenu |
setHeaderIcon(
Drawable icon)
Sets the submenu header's icon to the icon given in
icon
Drawable .设置子菜单标题图标
|
abstract SubMenu |
setHeaderIcon(int iconRes)
Sets the submenu header's icon to the icon given in
iconRes resource id.
设置子菜单标题图标
|
abstract SubMenu |
setHeaderTitle(
CharSequence title)
Sets the submenu header's title to the title given in
title.
设置子菜单标题
|
abstract SubMenu |
setHeaderTitle(int titleRes)
Sets the submenu header's title to the title given in
titleRes resource identifier.
设置子菜单标题
|
abstract SubMenu |
setHeaderView(
View view)
Sets the header of the submenu to the
View given in
view.设置指定的View对象作为子菜单图标
|
abstract SubMenu |
setIcon(
Drawable icon)
Change the icon associated with this submenu's item in its parent menu.设置子菜单在父菜单中显示的图标
|
abstract SubMenu |
setIcon(int iconRes)
Change the icon associated with this submenu's item in its parent menu.
设置子菜单在父菜单中显示的图标
|
使用案例:
1.strings.xml
<resources>
<string name="app_name">TestActivity</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="lable">您的选择为\n</string>
<string name="gender">性别</string>
<string name="male">男</string>
<string name="female">女</string>
<string name="hooby">爱好</string>
<string name="hooby1">游泳</string>
<string name="hooby2">唱歌</string>
<string name="hooby3">写java程序</string>
<string name="ok">确定</string>
</resources>
2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
>
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:editable="false"
android:cursorVisible="false"
android:text="@string/lable"/>
</ScrollView>
</LinearLayout>
3.Activity
package wyf.jc; //声明包语句
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 Sample_6_1 extends Activity {
final int MENU_GENDER_MALE=0; //性别为男选项编号
final int MENU_GENDER_FEMALE=1; //性别为女选项编号
final int MENU_HOBBY1=2; //爱好1选项编号
final int MENU_HOBBY2=3; //爱好2选项编号
final int MENU_HOBBY3=4; //爱好3选项编号
final int MENU_OK=5; //确定菜单选项编号
final int MENU_GENDER=6; //性别子菜单编号
final int MENU_HOBBY=7; //爱好子菜单编号每个菜单项目的编号=======end============
final int GENDER_GROUP=0; //性别子菜单项组的编号
final int HOBBY_GROUP=1; //爱好子菜单项组的编号
final int MAIN_GROUP=2; //外层总菜单项组的编号
MenuItem[] miaHobby=new MenuItem[3];//爱好菜单项组
MenuItem male=null;//男性性别菜单项
@Override
public void onCreate(Bundle savedInstanceState) { //重写onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //设置当前屏幕
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
//性别单选菜单项组 菜单若编组就是单选菜单项组
SubMenu subMenuGender = menu.addSubMenu(MAIN_GROUP,MENU_GENDER,0,R.string.gender);
subMenuGender.setIcon(R.drawable.gender);
subMenuGender.setHeaderIcon(R.drawable.gender);
male=subMenuGender.add(GENDER_GROUP, MENU_GENDER_MALE, 0, R.string.male);
male.setChecked(true);
subMenuGender.add(GENDER_GROUP, MENU_GENDER_FEMALE, 0, R.string.female);
//设置GENDER_GROUP组是可选择的,互斥的
subMenuGender.setGroupCheckable(GENDER_GROUP, true,true);
//爱好复选菜单项组
SubMenu subMenuHobby = menu.addSubMenu(MAIN_GROUP,MENU_HOBBY,0,R.string.hobby);
subMenuHobby.setIcon(R.drawable.hobby);
miaHobby[0]=subMenuHobby.add(HOBBY_GROUP, MENU_HOBBY1, 0, R.string.hobby1);
miaHobby[1]=subMenuHobby.add(HOBBY_GROUP, MENU_HOBBY2, 0, R.string.hobby2);
miaHobby[2]=subMenuHobby.add(HOBBY_GROUP, MENU_HOBBY3, 0, R.string.hobby3);
miaHobby[0].setCheckable(true);//设置菜单项为复选菜单项
miaHobby[1].setCheckable(true);
miaHobby[2].setCheckable(true);
//确定菜单项
MenuItem ok=menu.add(GENDER_GROUP+2,MENU_OK,0,R.string.ok);
OnMenuItemClickListener lsn=new OnMenuItemClickListener(){//实现菜单项点击事件监听接口
@Override
public boolean onMenuItemClick(MenuItem item) {
appendStateStr();
return true;
}
};
ok.setOnMenuItemClickListener(lsn);//给确定菜单项添加监听器
//给确定菜单项添加快捷键
ok.setAlphabeticShortcut('o');//设置字符快捷键
//ok.setNumericShortcut('1');//设置数字快捷键
//ok.setShortcut('a', '2');//同时设置两种快捷键
//要注意,同时设置多次时只有最后一个设置起作用
return true;
}
@Override //单选或复选菜单项选中状态变化后的回调方法
public boolean onOptionsItemSelected(MenuItem mi){
switch(mi.getItemId()){
case MENU_GENDER_MALE://单选菜单项状态的切换要自行写代码完成
case MENU_GENDER_FEMALE:
mi.setChecked(true);
appendStateStr();//当有效项目变化时记录在文本区中
break;
case MENU_HOBBY1://复选菜单项状态的切换要自行写代码完成
case MENU_HOBBY2:
case MENU_HOBBY3:
mi.setChecked(!mi.isChecked());
appendStateStr();//当有效项目变化时记录在文本区中
break;
}
return true;
}
//获取当前选择状态的方法
public void appendStateStr(){
String result="您选择的性别为:";
if(male.isChecked()){
result=result+"男";
}
else{
result=result+"女";
}
String hobbyStr="";
for(MenuItem mi:miaHobby){
if(mi.isChecked()){
hobbyStr=hobbyStr+mi.getTitle()+"、";
}
}
if(hobbyStr.length()>0){
result=result+",您的爱好为:"+hobbyStr.substring(0, hobbyStr.length()-1)+"。\n";
}
else{
result=result+"。\n";
}
EditText et=(EditText)Sample_6_1.this.findViewById(R.id.EditText01);
et.append(result);
}
}
程序运行图: