選單設定程式碼
發表人:
Seachaos
積分: 2432
積分: 2432
以下程式碼可以進行選單的配置
(在Activity物建內)
[quote]
private final int MENU_TEST = Menu.FIRST;
private final int MENU_QUIT = Menu.FIRST+1;
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0,MENU_TEST,0,"TEST");
menu.add(0,MENU_QUIT,0,"Quit");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case MENU_TEST:
break;
case MENU_QUIT:
finish();
break;
}
return true;
}
[/quote]
private final int MENU_TEST...
是定義選單選項的ID
public boolean onCreateOptionsMenu(Menu menu)
是建立選單動作,需要@Override覆寫Activity的onCreateOptionsMenu
public boolean onOptionsItemSelected(MenuItem item)
是選單被選取後的動作處理
(在Activity物建內)
[quote]
private final int MENU_TEST = Menu.FIRST;
private final int MENU_QUIT = Menu.FIRST+1;
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0,MENU_TEST,0,"TEST");
menu.add(0,MENU_QUIT,0,"Quit");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case MENU_TEST:
break;
case MENU_QUIT:
finish();
break;
}
return true;
}
[/quote]
private final int MENU_TEST...
是定義選單選項的ID
public boolean onCreateOptionsMenu(Menu menu)
是建立選單動作,需要@Override覆寫Activity的onCreateOptionsMenu
public boolean onOptionsItemSelected(MenuItem item)
是選單被選取後的動作處理