首页 > 技术文章 > andoid 监听返回键退出

xuhaifeng 2017-08-11 09:36 原文

//监听返回键
public void onBackPressed(){
confirmExit();
}
//确认退出程序对话
private void confirmExit()
{
//确认退出程序
new AlertDialog.Builder(this).setTitle(R.string.confirm_exit)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//点击'yes'后执行退出操作
MainActivity.this.finish();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//取消操作
}
}).show();
}

推荐阅读