原程式請參考[Android UI 教學(2)] 建立有圖示功能表請修訂下列文字
package com.example.menu;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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").setIcon(R.drawable.icon);
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("關於");
openAboutDialog();
break;
case EXIT_ID:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
public void openAboutDialog()
{
new AlertDialog.Builder(this)
.setTitle("關於")
.setMessage("南開科大")
//按下確認按鈕後的事件
.setPositiveButton("ok",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
}
)
.show();
}
}
沒有留言:
張貼留言