Android - 对话框 - 如果用户输入错误的密码,则保持对话框打开状态



我有一个对话框供用户输入解锁代码。如果他们输入错误的代码,我想保持盒子打开,以便他们有机会修复它。

AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Unlock Code");
    alert.setMessage("Please enter the unlock code.");

    final EditText input = new EditText(this);
    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText().toString();
     if(codeUnlock.checkCode(value)){ // checks the code that was put in
         Toast.makeText(MainActivity.this, "Thank you for purchasing.", Toast.LENGTH_LONG).show();
         yearPurchased = currentYear;
         checkForUpdate(false);
     }else{
         Toast.makeText(MainActivity.this, "Incorrect code.", Toast.LENGTH_LONG).show();

     }

      }
    });
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
      }
    });
    alert.show();

基本上,如果 checkCode 为假,我只想显示 toast 但不关闭窗口。

有什么建议吗?

谢谢。

你可以试试这个:

builder.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
    if(!errorFlag) {
             // you need this flag in order to close the dialog 
             // when there is no issue
             dialog.dismiss();
     }
}
});

设置正按钮:

builder.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
   Button b = builder.getButton(AlertDialog.BUTTON_POSITIVE);
       b.setOnClickListener(new View.OnClickListener() {
    @Override
public void onClick(View view) {
        // perform operations here
        // set the flag, that is isError = true or false
        // if false, call builder.dismiss();
    }
  }
});

设置负按钮:

Button n = builder.getButton(AlertDialog.BUTTON_NEGATIVE);
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
        // perform operations here too if needed.
     }
    }
   });
  }
});

创建如下所示的对话框:

final AlertDialog builder = new AlertDialog.Builder(YourActivity.this)
    .setNegativeButton("cancel", null)
    .setPositiveButton("ok", null)
    .create();

使用以下方式显示对话框:

builder.show();

也许这会有所帮助。只要添加一个onShowListener,你应该很好。我自己没有测试过,但从这里得到它:https://stackoverflow.com/a/7636468。但它仅适用于 API 8+。

alert.setPositiveButton(android.R.string.ok, null);
alert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
    Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Do something
            // Dismiss once everything is OK.
            d.dismiss();
        }
    });
}

我昨天遇到了这个问题,我通过覆盖正按钮的点击侦听器解决了它:

AlertDialog.Builder builder = new AlertDialog.Builder(
            context);
    builder.setTitle(R.string.my_title);
    builder.setView(getLayoutInflater().inflate(
            R.layout.my_dialog, null));
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setNegativeButton(android.R.string.cancel, null);
    final AlertDialog dialog = builder.create();
    dialog.show();
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText editTextUserId = (EditText) dialog
                            .findViewById(R.id.editTextUserId);
                    EditText editTextPassword = (EditText) dialog
                            .findViewById(R.id.editTextPassword);
                    if (editTextUserId.length() == 0
                            || editTextPassword.length() == 0) {
                        Toast.makeText(
                                context,
                                R.string.you_must_enter_username_and_password,
                                Toast.LENGTH_LONG).show();
                        return;
                    }
                    dialog.dismiss();
                }
            });
我知道

我参加聚会太晚了,但希望这有助于人们浏览谷歌。我的解决方案要求您在用户输入不正确的内容时抛出 IOException。

流程图:

流程图

我是这样做的:

用户在对话框中输入,然后按肯定按钮

builder.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        try {
            //do stuff if input was correct
            //if not, then throw an IOException
            dialog.dismiss();
            //dismiss, dialog goes away
        } catch (IOException e) {
            //User entered bad input, change the form to show feedback
            AlertDialog thisDialog = (AlertDialog) dialog;
            thisDialog.setTitle("Incorrect Format");
            thisDialog.setIcon(android.R.drawable.ic_dialog_alert);
            thisDialog.cancel();
            //cancel the dialog -> fire the OnCancelListener
        }
    }
});

假设它不正确,则触发OnCancelListener

builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        ((AlertDialog) dialog).show();
    }
});

这让我们回到了积极的按钮。

以防万一我们希望他们实际取消对话框而不是进入无限循环,当他们按下负按钮时,我们将关闭那里的对话框。

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});

真的希望这有帮助!

最新更新