我有一个对话框显示,我希望在按下后退按钮时要求用户确认他想在取消生效之前取消对话框。这是我的代码:
dialog.setOnCancelListener(new OnCancelListener(){
@Override
public void onCancel(DialogInterface arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(dialog.getContext());
builder.setMessage( "Are you sure you want to cancel?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface diag, int id) {
diag.dismiss();
dialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface diag, int id) {
diag.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
dialog.show();
问题是,当按下后退按钮时,我当前的对话框被取消,然后显示我的“确认消息”。如何确保在用户确认之前不会取消对话框?