package com.android.EX03_17;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 MainActivity extends Activity { public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, R.string.about);
menu.add(0, 1, 1, R.string.exit);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case 0:
openOptionsDialog();
break;
case 1:
exitOptionDialog();
break;
}
return true;
}
private void openOptionsDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.about)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
private void exitOptionDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.about)
.setMessage(R.string.exit_true)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).show();
} public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}----------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">按下menu按钮试试看 </string>
<string name="app_name">MyEX03_17</string>
<string name="about">关于</string>
<string name="exit">离开</string>
<string name="app_about_msg">这是大卫的范例练习</string>
<string name="ok">确认</string>
<string name="exit_true">确认要离开吗?</string>
<string name="cancel">取消</string>
</resources>
----------------------------------------------------------------------------------------------------------
模拟器上按menu键出现关于和离开..我想让它点离开的时候再弹出一个确认要离开的对话框..然后确认和取消..确认就退出,取消就返回上个Activity..可是我点离开的时候程序出错 异常..想不通
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;public class MainActivity extends Activity { public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, R.string.about);
menu.add(0, 1, 1, R.string.exit);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case 0:
openOptionsDialog();
break;
case 1:
exitOptionDialog();
break;
}
return true;
}
private void openOptionsDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.about)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
private void exitOptionDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.about)
.setMessage(R.string.exit_true)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).show();
} public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}----------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">按下menu按钮试试看 </string>
<string name="app_name">MyEX03_17</string>
<string name="about">关于</string>
<string name="exit">离开</string>
<string name="app_about_msg">这是大卫的范例练习</string>
<string name="ok">确认</string>
<string name="exit_true">确认要离开吗?</string>
<string name="cancel">取消</string>
</resources>
----------------------------------------------------------------------------------------------------------
模拟器上按menu键出现关于和离开..我想让它点离开的时候再弹出一个确认要离开的对话框..然后确认和取消..确认就退出,取消就返回上个Activity..可是我点离开的时候程序出错 异常..想不通
还有,你的onCreateOptionsMenu()和onOptionsItemSelected()不加@Override不报错吗?
第三个参数order :排序,这个item所在的位置