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

时间:2015-08-26 11:07:34

标签: android alertdialog android-alertdialog

我已成功在EditText中显示AlertDialog。在编辑字段中输入文本时,会将其与自定义文本进行比较。如果输入的文本不匹配,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();
                }
            });

4 个答案:

答案 0 :(得分:1)

您可以在条件中使用dialog.dismiss();

FYI

使用 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 :(得分:1)

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

  1. 使用DialogBu​​ilder创建对话框
  2. show()对话框
  3. 在显示的对话框中找到按钮并覆盖其onClickListener
  4. 所以,创建一个监听器类:

    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 - 您无法在其中添加自定义侦听器 - 如果在{之后未覆盖侦听器,则对话框仍会被忽略{1}}被调用。

答案 2 :(得分:0)

试试这个......

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

答案 3 :(得分:0)

将代码更改为第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();
          }
      }
  });