@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 按下键盘上返回按钮
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this)
.setTitle("关闭程序")
.setMessage("确定要关闭吗")
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
finish();
}
}).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
System.exit(0);
// 或者下面这种方式
// android.os.Process.killProcess(android.os.Process.myPid());
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 按下键盘上返回按钮
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this)
.setTitle("关闭程序")
.setMessage("确定要关闭吗")
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
finish();
}
}).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
System.exit(0);
// 或者下面这种方式
// android.os.Process.killProcess(android.os.Process.myPid());
}
本文介绍了一种在Android应用程序中实现自定义退出确认对话框的方法。当用户尝试通过返回键退出应用时,会弹出一个对话框询问用户是否真的想要退出。文中展示了如何使用AlertDialog.Builder来创建该对话框,并在对话框中提供了确定和取消两个选项。
619

被折叠的 条评论
为什么被折叠?



