我试图使用确认对话框返回,所以我决定处理onBackPressed()方法,然后我为确认对话框添加了带有null listener的确定和取消按钮的代码。
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirmation");
builder.setMessage("Are you sure want to exit?");
builder.setPositiveButton("OK",null);
builder.setNegativeButton("Cancel", null);
builder.show();
}
它一直工作得很好,但问题在于如果我点击确定,则应继续执行super.onBackPressed()的行为,否则它应该停留在同一个活动中。
以下是我尝试过的代码。
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirmation");
builder.setMessage("Are you sure want to exit?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//super.onBackPressed();
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}