无法解除AlertDialog。它不断涌现

时间:2014-01-27 17:35:07

标签: android alertdialog

我有一个带有EditText的自定义AlertDialog用于PIN。单击正面按钮,将使用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);
}

1 个答案:

答案 0 :(得分:0)

看起来你正在为每个触摸事件做这件事。如果您只尝试执行此操作一次,那么您应该检查传入的MotionEvent并仅在TouchDown上触发响应。您可能可能会根据在触地,触摸,移动等触发的事件调用多个警报对话框。这会使其外观再次打开,但实际上是因为彼此背后有多个。

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

为了使警报窗口保持打开状态,您可以按照这两篇文章中提到的一些建议进行操作:Tech TipsRe-create AlertDialog。两者都依赖于覆盖和创建自己的AlertDialog窗口。