【发布时间】:2011-03-10 19:01:07
【问题描述】:
如果每个页面都有一个按钮可以转到主页如何结束上一页,因为当我按主页按钮然后按返回按钮时我想退出程序
【问题讨论】:
标签: android button android-activity
如果每个页面都有一个按钮可以转到主页如何结束上一页,因为当我按主页按钮然后按返回按钮时我想退出程序
【问题讨论】:
标签: android button android-activity
在您的主页活动中,我假设当从应用程序的其他位置单击主页按钮时会显示该活动,您可以按下后退按钮并退出应用程序。下面将显示一个对话框,询问用户是否真的要退出。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
showDialog(DIALOG_REALLY_EXIT_ID);
return false;
}
@Override
protected Dialog onCreateDialog(int id)
{
final Dialog dialog;
switch(id)
{
case DIALOG_REALLY_EXIT_ID:
dialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Home.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).create();
break;
default:
dialog = null;
}
return dialog;
}
【讨论】: