代码之家  ›  专栏  ›  技术社区  ›  punchman

隐藏()后的Android AlertDialog show()

  •  1
  • punchman  · 技术社区  · 7 年前

    我有点麻烦 AlertDialog 。如果我打电话 dialog.show() 之后 dialog.hide() 不会显示,但如果我打电话 对话显示() 再一次,一切正常。如果我打电话 对话显示() 连续两次,始终显示对话框。

    如果更换 hide() -> dismiss() 一切都好。但在我的情况下,我需要使用 hide() 保存对话框。

    样品

    AlertDialog dialog;
    
    @Override
    protected void onCreate(@Nullable Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.activity_auth);
        dialog = new AlertDialog.Builder(this)
            .setTitle("Title")
            .setMessage("Text")
            .setPositiveButton("Yes", (dialogInterface, which) -> onYesClicked())
            .create();
        Button login = findViewById(R.id.btn_login);
        login.setOnClickListener(v -> dialog.show());
    }
    
    private void onYesClicked() {
        dialog.hide();
    }
    

    已编辑:解决方案

    private void onYesClicked() {
        new Handler().post(() -> dialog.hide());
    }
    
    5 回复  |  直到 7 年前
        1
  •  0
  •   Cao Minh Vu    7 年前

    如果使用,hide(),可能会导致泄漏窗口错误。请看一下: Activity has leaked window that was originally added

    使用disclose()并保存消息/标题,而不是整个对话框。

        2
  •  0
  •   yu wang    7 年前

    如果要再次使用“隐藏并显示”对话框,请尝试以下代码:

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface d) {
            dialog.show();
        }
    });
    

    调用hide和show时,回调将被调用。

        3
  •  0
  •   user9064724 user9064724    7 年前

    尝试此操作以使对话框仅弹出一次:

     if (dialog != null && dialog.getDialog() != null
                                    && dialog.getDialog().isShowing()) {
                         //Leave Empty here or your way
    }else{
     code to open a dialog
    }
    
        4
  •  0
  •   Mobile Team ADR-Flutter    7 年前

    对下面代码中使用的onYesClicked()方法进行一些更改。。

    private void onYesClicked() {
        new Handler().postDelayed(new Runnable() {
    
            // Showing message with a timer.
            @Override
            public void run() {
                dialog.hide();
            }
        }, 1000);
    
    }
    
        5
  •  0
  •   punchman    7 年前

    我解决了这个问题。方法 hide() 当按钮(由 AlerdDialog.Builder )已单击。因为系统发送MSG\u disclose\u对话框并自动调用 dismiss() 对于此对话框:

    SDK代码:

            // Post a message so we dismiss after the above handlers are executed
            mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialogInterface)
                    .sendToTarget();