幽映每白日,清辉照衣裳。这篇文章主要讲述android 之 菜单相关的知识,希望能为你提供帮助。
android的菜单主要分三类:选项菜单(Options Menu)、上下文菜单(Context Menu)、子菜单(Submenu)。
1 选项菜单和子菜单
一个Menu对象代表一个菜单,Menu中可添加菜单项MenuItem,也可以添加子菜单SubMenu。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
//添加性别子菜单
SubMenu subMenuGender=menu.addSubMenu(MAIN_GROUP, MENU_GENDER, 0,"性别");
subMenuGender.setIcon(R.drawable.gender);
subMenuGender.setHeaderIcon(R.drawable.gender);
male=subMenuGender.add(GENDER_GROUP,MENU_GENDER_MALE, 0, "男"); //为子菜单添加选项
male.setChecked(true); //设置默认选项
subMenuGender.add(GENDER_GROUP,MENU_GENDER_FEMALE, 0, "女"); //为子菜单添加选项
subMenuGender.setGroupCheckable(GENDER_GROUP, true, true); //设置GENDER_GROUP组是可选的、互斥的
//添加兴趣子菜单
SubMenu subMenuHobby=menu.addSubMenu(MAIN_GROUP, MENU_HOBBY, 1, "爱好");
subMenuHobby.setIcon(R.drawable.hobby);
miaHobby[0]=subMenuHobby.add(HOBBY_GROUP, MENU_HOBBY1, 0, "游泳");
miaHobby[1]=subMenuHobby.add(HOBBY_GROUP, MENU_HOBBY2, 1, "篮球");
miaHobby[2]=subMenuHobby.add(HOBBY_GROUP, MENU_HOBBY3, 2, "跑步");
miaHobby[0].setCheckable(true);
miaHobby[1].setCheckable(true);
miaHobby[2].setCheckable(true); //设置为可多选
MenuItem ok=menu.add(MAIN_GROUP, MENU_OK, 3, "确定"); //为主菜单添加选项菜单
OnMenuItemClickListener lsn=new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
appendStateStr();
return true;
}
};
ok.setOnMenuItemClickListener(lsn);
ok.setAlphabeticShortcut(\'o\'); //给确定菜单设置字符快捷键
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
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;
}
private void appendStateStr() {
// TODO Auto-generated method stub
String result="您选择的性别是:";
if(male.isChecked()){
result+="男";
}else{
result+="女";
}
String hobbyStr="";
for(MenuItem mi:miaHobby){
if(mi.isChecked()){
hobbyStr+=mi.getTitle()+"、";
}
}
if(hobbyStr.length()> 0){
result+=",您的爱好为:"+hobbyStr.substring(0,hobbyStr.length()-1)+".\\n";
}else{
result=result+".\\n";
}
Toast.makeText(mainActivity.this, result, Toast.LENGTH_LONG).show();
}
文章图片
文章图片
文章图片
文章图片
2 上下文菜单
上下文菜单是注册到某个View对象上的,如果一个View对象注册了上下文菜单,用户可通过长按该View对象弹出上下文菜单.
首先在onCreateContextMenu中为不同空间配置不同的上下文菜单:
@Override在onCreate方法中为两个文本框注册上下文菜单:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderIcon(R.drawable.bird);
if(v==findViewById(R.id.editview01)){
menu.add(0,1,0,"菜单项1");
menu.add(0,2,0,"菜单项2");
menu.add(0,3,0,"菜单项3");
}else if(v==findViewById(R.id.editview02)){
menu.add(0,4,0,"菜单项4");
menu.add(0,5,0,"菜单项5");
}
}
@Override添加上下文菜单监听事件:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.registerForContextMenu(findViewById(R.id.editview01));
this.registerForContextMenu(findViewById(R.id.editview02));
}
@Override【android 之 菜单】
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case 1:
case 2:
case 3:
Toast.makeText(mainActivity.this, "这是第一个EditView的上下文菜单"+item.getTitle(), Toast.LENGTH_LONG).show();
case 4:
case 5:
Toast.makeText(mainActivity.this, "这是第二个EditView的上下文菜单"+item.getTitle(), Toast.LENGTH_LONG).show();
}
return super.onContextItemSelected(item);
}
文章图片
文章图片
推荐阅读
- ASP.NET Web Application中使用链接文件
- android 之 GridView
- android 之 service
- android 之 View
- android 之 TabHost
- android屏幕适配的全攻略
- android 之 ListView相关
- Dijkstra的算法分析
- 离散数学二叉树