【问题标题】:Confirming whether user really wants to close the dialog确认用户是否真的想关闭对话框
【发布时间】:2018-07-10 12:55:08
【问题描述】:

我有一个AlertDialog。通过安装DialogInterface.OnCancelListener,我可以在取消对话框时收到通知。但是,当用户按下 BACK 按钮或在对话框区域外点击时,我看不出有任何方法可以阻止 Android 自动关闭对话框。

假设每当用户尝试取消对话框时,我想显示另一个对话框,询问“您确定要关闭此对话框吗?”。我以为我可以在onCancel()DialogInterface.OnCancelListener 中实现它,但它不起作用,因为Android 总是自动关闭对话框。有没有办法阻止 Android 这样做,以便我可以选择是否要关闭它?

【问题讨论】:

标签: android android-alertdialog android-dialog


【解决方案1】:

试试这个:

private void openMainDialog() {
    new AlertDialog.Builder(this)
            .setTitle("Some title")
            .setMessage("Some Message")
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            })
            .setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    openOnCancelMainDialog();
                }
            })
            .show();
}
private void openOnCancelMainDialog() {
    new AlertDialog.Builder(this)
            .setTitle("Warning")
            .setMessage("Do you really want to close the dialog?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    openMainDialog();
                }
            })
            .show();
}

【讨论】:

  • 但是我必须手动处理关闭逻辑。对于 BACK 按钮,这可能很容易通过覆盖onBackPressed 来实现,但是在对话框区域之外点击以取消它呢?我应该如何检测到这一点?
  • 我以为这就是你想要的?如果用户点击外部或后退按钮,防止对话框关闭。
  • 不完全。我希望通过点击外部或按下后退按钮而不是立即取消对话框。当用户点击外部或按下后退按钮时,我首先要显示一个确认对话框,询问:“你真的要关闭对话框吗?”我正在寻找一种方法来实现这一点。
  • 谢谢,但这不起作用。它显示确认对话框,但主对话框首先消失。我希望主对话框保持打开状态,并且确认对话框应显示在其顶部。
【解决方案2】:

但是,当用户按下“返回”按钮或在对话框区域外点击时,我看不出有任何方法可以阻止 Android 自动关闭对话框。

有办法阻止它,

 builder.setCancelable(false);

如果这就是你要找的东西,那么你得到了。如果您需要任何进一步的详细信息/问题,请在此处发表评论,因为您的问题在那之后并不清楚。

【讨论】:

    【解决方案3】:

    您可以禁用“随机”取消对话框。使用这个method

    builder.setNegativeButton("Cancel", (dialog, which) -> {
        dialog.dismiss();
    })    
    builder.setCancellable(false);
    

    或者你可以使用这个method

    AlertDialog dialog = builder.show();
    dialog.setCancelableOnTouchOutside(false);
    

    【讨论】:

      【解决方案4】:
      > Try this code it works for me
      
      
      
      
      
      
       private void createDialog() {
      
          AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
      
      
      
          alertDlg.setMessage("Are you sure you want to exit?");
      
          alertDlg.setCancelable(false); // We avoid that the dialog can be canceled
      
      , forcing the user to choose one of the options
      
      
      
          alertDlg.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
      
      
      
            public void onClick(DialogInterface dialog, int id) {
      
                     TestBackActivity.super.onBackPressed();
      
                 }
      
          }
      
           );
      
           alertDlg.setNegativeButton("No", new DialogInterface.OnClickListener() {
      
          @Override
      
          public void onClick(DialogInterface dialog, int which) {
      
            // We do nothing
      
          }
      
          });
      
           alertDlg.create().show();
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-28
        相关资源
        最近更新 更多