本文实例讲述了android选项菜单用法。分享给大家供大家参考。具体如下:
android平台下所提供的菜单大体上可分为三类:选项菜单、上下文菜单和子菜单。
当activity在前台运行时,如果用户按下手机上的menu键,此时就会在屏幕低端弹出相应的选项菜单。但这个功能需要开发人员编程来实现的,如果在开发应用程序时没有实现该功能,那么程序运行时按下手机的meun键是不会起作用的。
对于携带图标的选项菜单,每次最多只能显示6个,当菜单选项多于6个时,将只显示前5个和一个扩展菜单选项,点击扩展菜单选项将会弹出其余的菜单项。扩展菜单项中将不会显示图标,但是可以显示单选按钮和复选框。
以下模拟选项菜单
main.xml布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?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= "您的选择为\n" >
</edittext>
</scrollview>
</linearlayout>
|
meunactivity类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
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 ]; // 爱好菜单项组
menuitem malemenuitem = null ; // 男性菜单项
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
}
/**j
* 初始化选项菜单
*/
@override
public boolean oncreateoptionsmenu(menu menu) {
//单选菜单选项
submenu gendermenu = menu.addsubmenu(main_group, menu_gender, 0 , "性别" );
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 , "女" );
//设置菜单项为单选菜单项,互斥的
gendermenu.setgroupcheckable(gender_group, true , true );
//复选菜单选项
submenu hobbymenu = menu.addsubmenu(main_group,menu_hobby, 0 , "爱好" );
hobbymenu.seticon(r.drawable.hobby);
hobbymenu.setheadericon(r.drawable.hobby);
hoddymenuitems[ 0 ]=hobbymenu.add(hobby_group, menu_hobby1, 0 , "游泳" );
hoddymenuitems[ 1 ]=hobbymenu.add(hobby_group, menu_hobby2, 0 , "唱歌" );
hoddymenuitems[ 2 ]=hobbymenu.add(hobby_group, menu_hobby3, 0 , "编程" );
//设置菜单项为复选菜单项
hoddymenuitems[ 0 ].setcheckable( true );
hoddymenuitems[ 1 ].setcheckable( true );
hoddymenuitems[ 2 ].setcheckable( true );
//确定菜单项
menuitem ok=menu.add(main_group,menu_ok, 0 , "确定" );
ok.setonmenuitemclicklistener( new onmenuitemclicklistener(){
public boolean onmenuitemclick(menuitem item) {
appendstatestr();
return true ;
}
});
//给确定菜单项添加快捷键
ok.setalphabeticshortcut( 'o' ); //设置字符快捷键
//ok.setnumericshortcut('1');//设置数字快捷键
//ok.setshortcut('a', '2');//同时设置两种快捷键。注意:同时设置多次时只有最后一个设置起作用
return true ; // 记得返回true,否则无效
}
/**
* 在菜单选项中某个选项被选中时调用该事件
*/
@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 = "您选择的性别为:" ;
if (malemenuitem.ischecked()) {
result = result + "男" ;
} else {
result = result + "女" ;
}
string hobbystr = "" ;
for (menuitem hoddy : hoddymenuitems) {
if (hoddy.ischecked()) {
hobbystr = hobbystr + hoddy.gettitle() + "、" ;
}
}
if (hobbystr.length() > 0 ) {
result = result + ",您的爱好为:"
+ hobbystr.substring( 0 , hobbystr.length() - 1 ) + "。\n" ;
} else {
result = result + "。\n" ;
}
edittext et = (edittext) meunactivity. this .findviewbyid(r.id.edittext);
et.append(result);
}
}
|
运行结果:
希望本文所述对大家的android程序设计有所帮助。