Menu创建:
@Override//这里遇到一个问题add的是MenuItem的id public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.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 stub if(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选择:
@Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub return super.onOptionsItemSelected(item); }
Menu关闭:
@Override public void onOptionsMenuClosed(Menu menu) { // TODO Auto-generated method stub super.onOptionsMenuClosed(menu); if(isSelect) isSelect=false; else isSelect=true; Log.e("Menu", "onOptionsMenuClosed"); }
结果: