【问题标题】:AlertDialog poping up twiceAlertDialog 弹出两次
【发布时间】:2018-06-18 02:37:30
【问题描述】:

我在createview() 方法中有一个fragment,我设置了一个条件,如果此条件为真,则显示alertdialog 并单击其任何按钮,关闭对话框,但单击对话框的按钮时再次弹出对话框,此处是我的条件,里面有dialog

 if(getUser().isFirstTimeLogin() && getUser().getReceivedRequests().size() > 0 && getUser().getReceivedRequests().get(0).getStatus() == 0){

        dialog = new AlertDialog.Builder(getActivity()).create();
        LayoutInflater layoutInflater = getLayoutInflater();
        View dialogView = layoutInflater.inflate(R.layout.anonymous_login_popup, null);
        TextView title = (TextView) dialogView.findViewById(R.id.title);
        TextView description = (TextView) dialogView.findViewById(R.id.tv_anonymous_dialog_content);
        TextView okBtn = (TextView) dialogView.findViewById(R.id.okBtn);
        TextView cancelBtn = (TextView) dialogView.findViewById(R.id.cancelBtn);
        title.setText("Pending Request");
        description.setText("Your Spouse request is pending");
        okBtn.setText("Accept");
        cancelBtn.setText("Reject");
        Typeface tf = FontManager.getTypeface(getActivity(), FontManager.VARELA_ROUND);
        FontManager.setContainerTypeface(dialogView, tf);
        dialog.setView(dialogView);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.show();
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();                   acceptRejectRequest(String.valueOf(getUser().getReceivedRequests().get(0).getId()), 1);
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();                    acceptRejectRequest(String.valueOf(getUser().getReceivedRequests().get(0).getId()),     2);
            }
        });

    }

【问题讨论】:

  • 您发布的代码是否可能被调用了两次??
  • 你确定已经没有两个对话框了吗?我觉得你的代码调用了两次。
  • 检查两个对话框是否堆叠在一起或在您关闭对话框后立即再次弹出?
  • @Jim 我已经调试过,代码没有运行两次
  • @ADM 我已经调试过了,代码没有运行两次

标签: android android-alertdialog android-dialog


【解决方案1】:

总是调试你的代码,看看代码是如何执行的,在这种情况下,代码运行两次,对话框相互堆叠,所以关闭上面的对话框会弹出它下面的对话框,所以看起来对话框会弹出多次:)

编码愉快!

【讨论】:

  • 调试成功了。显然,我有一个 onTouchListener 在后台调用 performClick() 操作,因此我调用了两个警报对话框。
【解决方案2】:

我使用和完美工作的方式是定义一个 AlertDialog 而不是 AlertDialog.Builder 我显示了 AlertDialog

这是我的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.DialogTheme);
LayoutInflater inflater = LayoutInflater.from(this);
View contentView = inflater.inflate(R.layout.custom_layout , null);
builder.setView(contentView);
AlertDialog alert = builder.create();
...
//instead of builder.show()
alert.show();

点击时只需使用alert.dismiss();

【讨论】:

    【解决方案3】:

    就显示对话框而言,我总是在 utils 类中设置一个类似 dialogIsDisplayed 的标志,一个布尔值。每当有调用对话框的事件时,我会在此标志为false 时显示对话框。立即,即调用对话框后,此标志设置为true

    在对话框内部,在调用dismiss() 之前,此标志设置为false

    这样可以防止创建重复的对话框。

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多