Menu创建:
@Override//这里遇到一个问题add的是MenuItem的idpublic boolean onCreateOptionsMenu(Menu menu) {// TODO Auto-generated method stubmenu.add(0,1023, 0, "一");menu.add(0,1022, 1, "开启线程");Log.e("onCreateOptionsMenu", Menu.FIRST+"");Log.e("onCreateOptionsMenu", String.valueOf(Menu.FIRST+1));return true;}
Menu准备:
@Override//这里getItem()这个却是通过item的索引public boolean onPrepareOptionsMenu(Menu menu) {// TODO Auto-generated method stubif(isSelect) menu.getItem(1).setTitle("开始服务");else menu.getItem(1).setTitle("停止服务");//Log.e("onPrepareOptionsMenu", Menu.FIRST+"");//Log.e("onPrepareOptionsMenu", String.valueOf(Menu.FIRST+1));return super.onPrepareOptionsMenu(menu);}
Menu选择:
@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stubreturn super.onOptionsItemSelected(item);}
Menu关闭:
@Overridepublic void onOptionsMenuClosed(Menu menu) {// TODO Auto-generated method stubsuper.onOptionsMenuClosed(menu);if(isSelect) isSelect=false;else isSelect=true;Log.e("Menu", "onOptionsMenuClosed");}
结果: