2. 在下圖中第一個紅色框位置上按下滑鼠右鍵,選擇「Source」選單,再選「Override/Implement Method」。
3. 把onCreateOptionsMenu()和onOptionsItemSelected()兩個事件程序打勾,按下OK鍵後,會產生相對應的事件處理程序的框架。
4. 指令說明
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
功能:增加功能選單
groupId: 群組代碼
itemId:選項代碼
order:選單次序
title:選單標題
5. 輸入下列程式(藍色)
package com.example.menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MenuDesignActivity extends Activity {
/** Called when the activity is first created. */
public static final int ADD_ID = Menu.FIRST ;
public static final int DELETE_ID = Menu.FIRST + 1;
public static final int ABOUT_ID = Menu.FIRST + 2;
public static final int EXIT_ID = Menu.FIRST + 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, ADD_ID, 0, "Add");
menu.add(0, DELETE_ID, 1, "Del");
menu.add(0, ABOUT_ID, 2, "About");
menu.add(0, EXIT_ID, 3, "Exit");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case ADD_ID:
setTitle("新增項目");
break;
case DELETE_ID:
setTitle("刪除項目");
break;
case ABOUT_ID:
setTitle("關於");
break;
case EXIT_ID:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
沒有留言:
張貼留言