“单击后退按钮后,“我的警报”对话框将显示一小段时间,活动结束。我的代码是:”
@Override
public void onBackPressed() {
super.onBackPressed();
showAlertDialog();
}
private void showAlertDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("WARNING")
.setCancelable(false)
.setMessage("You won't be able to get saved messages.")
.setPositiveButton("exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.create().show();
}
我希望在单击“后退”按钮时显示我的警报,用户可以选择停留还是结束。