如果输入错误的密码,继续显示 AlertDialog

Continue displaying AlertDialog if entered wrong password

我已经在 AlertDialog 中成功显示了一个 EditText。在编辑字段中输入文本时,会将其与自定义文本进行比较。如果输入的文本不匹配,AlertDilaog 应该继续显示,但当前对话框正在关闭单击肯定按钮,即使输入了错误的密码。

你们有解决办法吗?

更新: 这是代码

builder.setView(v)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    EditText enteredPassword = (EditText) v.findViewById(R.id.enteredPassword);
                    if (enteredPassword.getText().toString().trim().equals(correctPassword.trim()) {
                        Log.i(TAG, "User Entered Right Answer");
                      } else {
                        Log.i(TAG, "User Entered Wrong Answer");
                        // Continue showing the dialog if Wrong Answer is entered
                        Toast.makeText(getApplicationContext(), "Enter a proper answer", Toast.LENGTH_LONG).show();
                    }
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

试试这个...

dialogBuilder.setPositiveButton(R.string.ok, new OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            if(is text correct)
            {
                dialog.dismiss();
            }
        }
    });

您可以根据自己的情况使用dialog.dismiss();

仅供参考

使用dialog.dismiss();相反 dialog.cancel();

最后点个赞

if (enteredPassword.getText().toString().trim().equals(correctPassword.getText().toString().trim()) {
                    Log.i(TAG, "User Entered Right Answer");
                      dialog.dismiss();
                  } else {
                    Log.i(TAG, "User Entered Wrong Answer");
                    // Continue showing the dialog if Wrong Answer is entered
                    Toast.makeText(getApplicationContext(), "Enter a proper answer", Toast.LENGTH_LONG).show();
                }

请看

http://developer.android.com/reference/android/app/Dialog.html#dismiss()

是的,你可以。您基本上需要:

  1. 使用 DialogBu​​ilder 创建对话框
  2. 显示() 对话框
  3. 在显示的对话框中找到按钮并覆盖它们的 onClickListener

所以,创建一个监听器class:

class CustomListener implements View.OnClickListener {
    private final Dialog dialog;
    public CustomListener(Dialog dialog) {
        this.dialog = dialog;
    }
    @Override
    public void onClick(View v) {

        // Do whatever you want here

        // If tou want to close the dialog, uncomment the line below
        //dialog.dismiss();
    }
}

然后在显示对话框时使用:

AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));

请记住,您需要显示对话框,否则将无法找到按钮。此外,请务必将 DialogInterface.BUTTON_POSITIVE 更改为您用于添加按钮的任何值。另请注意,在 DialogBuilder 中添加按钮时,您需要提供 onClickListeners - 但是您不能在其中添加自定义侦听器 - 如果您在 show() 被调用。

将代码更改为 第 1 部分,并在显示对话框后立即使用 第 2 部分

第 1 部分

builder.setView(v)
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // Don't do anything here. Will override it later     
           }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        });

第 2 部分

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
  {            
      @Override
      public void onClick(View v)
      {
          EditText enteredPassword = (EditText) v.findViewById(R.id.enteredPassword);
          if (enteredPassword.getText().toString().trim().equals(correctPassword.getText().toString().trim()) {
              Log.i(TAG, "User Entered Right Answer");
              disalog.dismiss();
          } else {
              Log.i(TAG, "User Entered Wrong Answer");
              // Continue showing the dialog if Wrong Answer is entered
              Toast.makeText(getApplicationContext(), "Enter a proper answer", Toast.LENGTH_LONG).show();
          }
      }
  });