来自:http://www.mobilesdn.com/?action-viewnews-itemid-107
//创建三组菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//游戏进入菜单界面需要显示的菜单项目
menu.add(MENU_GROUP_MENU, MENU_HELP, ++menu_order, R.string.menu_help).setIcon(R.drawable.icon);
menu.add(MENU_GROUP_MENU, MENU_START, ++menu_order, R.string.menu_start).setIcon(R.drawable.icon);
menu.add(MENU_GROUP_MENU, MENU_ABOUT, ++menu_order, R.string.menu_about).setIcon(R.drawable.icon);
menu.add(MENU_GROUP_MENU, MENU_OTHER, ++menu_order, R.string.menu_other).setIcon(R.drawable.icon);
menu.add(MENU_GROUP_MENU, MENU_SET, ++menu_order, R.string.menu_set).setIcon(R.drawable.icon);
//游戏过程中的菜单项目
menu.add(MENU_GROUP_GAME, MENU_PAUSE, ++menu_order, R.string.menu_pause).setIcon(R.drawable.icon);
menu.add(MENU_GROUP_GAME, MENU_RESUME, ++menu_order, R.string.menu_resume).setIcon(R.drawable.icon);
menu.add(MENU_GROUP_GAME, MENU_HINT, ++menu_order, R.string.menu_hint).setIcon(R.drawable.icon);
menu.add(MENU_GROUP_GAME, MENU_EXIT, ++menu_order, R.string.menu_exit).setIcon(R.drawable.icon);
//其他状态下显示的菜单项目
menu.add(MENU_GROUP_HELP, MENU_GOMENU, ++menu_order, R.string.menu_gomenu).setIcon(R.drawable.icon);
menu.add(MENU_GROUP_HELP, MENU_RESUME, ++menu_order, R.string.menu_gogame).setIcon(R.drawable.icon);
menu.add(MENU_GROUP_HELP, MENU_EXIT, ++menu_order, R.string.menu_exit).setIcon(R.drawable.icon);
//一进来是在选单界面,所以设定其他组别菜单不可见
menu.setGroupVisible(MENU_GROUP_GAME, false);
menu.setGroupVisible(MENU_GROUP_HELP, false);
return true;
}
//动态修改在这里................
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
switch(gameState)
{
case GAME_STATE_MENU:
menu.setGroupVisible(MENU_GROUP_MENU, true);
menu.setGroupVisible(MENU_GROUP_GAME, false);
menu.setGroupVisible(MENU_GROUP_HELP, false);
break;
case GAME_STATE_HELP:
menu.setGroupVisible(MENU_GROUP_MENU, false);
menu.setGroupVisible(MENU_GROUP_GAME, false);
menu.setGroupVisible(MENU_GROUP_HELP, true);
break;
case GAME_STATE_ABOUT:
menu.setGroupVisible(MENU_GROUP_MENU, false);
menu.setGroupVisible(MENU_GROUP_GAME, false);
menu.setGroupVisible(MENU_GROUP_HELP, true);
break;
default:
menu.setGroupVisible(MENU_GROUP_MENU, false);
menu.setGroupVisible(MENU_GROUP_GAME, true);
menu.setGroupVisible(MENU_GROUP_HELP, false);
break;
}
return true;
}
//点击菜单之后的效果
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_HELP:
//点击帮助菜单
initHelp();
return true;
case MENU_START:
initGame();
return true;
case MENU_ABOUT:
initAbout();
return true;
case MENU_OTHER:
return true;
case MENU_SET:
return true;
}
return false;
}