@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.// getMenuInflater()... ...
@Override 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); MenuItem menuSys = menu.add(1001, 100, 1, "系統菜單"); menuSys.setTitle("菜單一"); /* 最多可以顯示6個子菜單 */ MenuItem menuUser = menu.add(1001, 101, 1, "用戶菜單"); menuUser.setTitle("菜單二"); menuUser.setShortcut('c', 'c'); /* 給菜單設置快捷鍵 */ MenuItem menuCust = menu.add(1001, 102, 1, "客戶菜單"); menuCust.setTitle("菜單三"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case 100: Toast.makeText(MainActivity.this, "選擇了菜單一", 1).show(); Intent intent = new Intent(MainActivity.this, NextActivity.class); item.setIntent(intent); /* 切換到第二個Activity */ break; case 101: Toast.makeText(MainActivity.this, "選擇了菜單二", 1).show(); break; case 102: Toast.makeText(MainActivity.this, "選擇了菜單三", 1).show(); break; default: break; } return super.onOptionsItemSelected(item); }