1、Menu的一级菜单与二级菜单的新建,以及单选与复选。值得一提,复选需要对状态进行手工设定
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SubMenu sub1 = menu.addSubMenu(0, 1, 0, "您的性别是");
SubMenu sub2 = menu.addSubMenu(0, 1, 0, "喜欢的书是");
// SUB1
sub1.setHeaderIcon(R.drawable.ic_launcher);
sub1.setHeaderTitle("请选择您的性别");
sub1.addSubMenu(0, FEMALE, 0, "女士");
sub1.addSubMenu(0, MALE, 0, "男士");
sub1.setGroupCheckable(0, true, true);
// SUB2
items[0] = sub2.add(1, Green, 0, book[0]).setCheckable(true);
items[1] = sub2.add(1, RED, 0, book[1]).setCheckable(true);
return true;
}
public boolean onOptionsItemSelected(MenuItem mi) {
switch (mi.getItemId()) {
case MALE:
edittext.setText("您选择的是:男士");
mi.setChecked(true);
break;
case FEMALE:
edittext.setText("您选择的是:女士");
mi.setChecked(true);
break;
case Green:
if (mi.isChecked()) {
mi.setChecked(false);
} else {
mi.setChecked(true);
}
break;
case RED:
if (mi.isChecked()) {
mi.setChecked(false);
} else {
mi.setChecked(true);
}
break;
}
return true;
}