我已经制作了一些菜单项,当我点击我的Android手机的菜单按钮时弹出项目。但我希望,当我进入活动时,菜单项将自动弹出而不点击菜单按钮......
public class SalesTrackerRoot extends Activity {
private static final int ORDER_ID = Menu.FIRST+1;
private static final int STORE_ID = Menu.FIRST+2;
private static final int SETTINGS_ID = Menu.FIRST+3;
private SalesTrackerDBAdapter mDbHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.salestrackerroot);
mDbHelper=new SalesTrackerDBAdapter(this);
mDbHelper.open();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, ORDER_ID, Menu.NONE, "Order List").setAlphabeticShortcut('o');
menu.add(Menu.NONE, STORE_ID, Menu.NONE, "Store Entry").setAlphabeticShortcut('s');
menu.add(Menu.NONE, SETTINGS_ID, Menu.NONE, "Settings").setAlphabeticShortcut('e');
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ORDER_ID:
startActivity(new Intent(this, SalesOrderList.class));
return(true);
case STORE_ID:
startActivity(new Intent(this, AddNewStoreName.class));
return(true);
case SETTINGS_ID:
startActivity(new Intent(this, SalesTrackerSettings.class));
return(true);
}
return(super.onOptionsItemSelected(item));
}
答案 0 :(得分:2)
在onCreate中只需调用openOptionsMenu()。