【问题标题】:Can not dismiss the AlertDialog. It keeps poping up无法关闭 AlertDialog。它不断弹出
【发布时间】:2014-01-27 17:35:06
【问题描述】:

我有一个自定义 AlertDialog,其中包含 PIN 的 EditText。 OnClick 的肯定按钮使用 SharedPreferences 检查editText 中的 PIN。如果匹配,我想关闭对话框,否则它应该保持打开状态。 当 PIN 正确时,对话框关闭并重新出现,我不希望它重新出现。 提前感谢您的帮助。

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub

    if ((System.currentTimeMillis() - mainScreenActivity.lastLoggedIn) / 1000 >= 120) {
        //startActivity(pinVarificationActivity);
        //Toast.makeText(getApplicationContext(),"Session has timed out, please enter your PIN",Toast.LENGTH_LONG).show();

        LayoutInflater inflaterPinVerificationDialog = this.getLayoutInflater();
        final View inflatorPinVerificationDialog = inflaterPinVerificationDialog.inflate(R.layout.dialog_pin_verification, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
        builder.setTitle("Session timed out. Please enter PIN");
        builder.setView(inflatorPinVerificationDialog);
        pinFromDialog = (EditText) inflatorPinVerificationDialog.findViewById(R.id.etDialogPin);

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                String dialogPinValue = pinFromDialog.getText().toString();

                String sharedPrefPinVal = loginData.getString("pin", "not found");
                if (sharedPrefPinVal.equals(dialogPinValue)) {
                    Toast.makeText(getApplicationContext(), "login successful",
                            Toast.LENGTH_SHORT).show();                     
                    mainScreenActivity.lastLoggedIn = System.currentTimeMillis();
                    alertDialogPinVerification.dismiss();


                } else {
                    Toast.makeText(getApplicationContext(),
                            "Incorrect pin - Please try again",
                            Toast.LENGTH_LONG).show();
                }

            }
        });
        builder.setNegativeButton("Forgot PIN", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        alertDialogPinVerification = builder.create();

        alertDialogPinVerification.show();

    } else {            
        mainScreenActivity.lastLoggedIn = System.currentTimeMillis();
    }
    return super.dispatchTouchEvent(ev);
}

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    看起来好像您在为每个触摸事件都这样做。如果您只尝试这样做一次,那么您可能应该检查传入的 MotionEvent 并仅在 TouchDown 上触发响应。您可能会根据触碰、触碰、移动等触发的事件调用多个警报对话框。这会使它看起来再次打开,但实际上是因为它们后面有多个。

    @Override
        public boolean dispatchTouchEvent(MotionEvent ev) 
        {
            if(ev.getAction() == MotionEvent.ACTION_DOWN)
            {
    

    为了使警报窗口保持打开状态,您可以遵循这两篇文章中提到的几个建议:Tech TipsRe-create AlertDialog。两者都依赖于覆盖和创建您自己的 AlertDialog 窗口。

    【讨论】:

    • 嗨,Jay,感谢您的回复,它现在对我有用。目前,它取消了带有 toast 消息的对话框作为不正确的 pin。如果我不想在 PIN 错误时取消对话框或如何重新打开 PIN 输入对话框,你能建议我该怎么做吗
    猜你喜欢
    • 2015-07-16
    • 2012-05-09
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多