用于多个 EditText 字段的 TextWatcher

TextWatcher for multiple EditText fields

我有一些使用 Material 对话框库的代码;我有一个包含四个 EditText 字段(电子邮件、姓名、用户名、密码)的对话框。我想要它以便禁用对话框的 Register 按钮,除非所有字段中至少有一个字符。

我四处寻找,找到了一种方法;不幸的是,它似乎不起作用。例如,如果我拉出对话框并在任何单个字段中键入任何内容,该按钮就会启用。但是,如果我编辑一个字段,然后编辑另一个字段,然后删除其中一个字段中的文本——该按钮将被禁用;如果我要填写所有字段,然后删除单个字段中的文本,也会发生同样的事情。

我曾考虑过利用另一个建议(这将涉及编写我自己的私有内部 class)——但我认为这无关紧要,因为我会实现同样的目标(在至少,据我所知)。

        //registerDialog is a MaterialDialog object
        final View registerAction = registerDialog.getActionButton(DialogAction.POSITIVE);
        final EditText registerNameInput;
        final EditText registerEmailInput;
        final EditText registerUsernameInput;
        final EditText registerPasswordInput;
        if (registerDialog.getCustomView() != null) {
            registerNameInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_name);
            registerEmailInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_email);
            registerUsernameInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_username);
            registerPasswordInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_password);

            /*
             * TextWatcher lets us monitor the input fields while registering;
             * This make sure we don't allow the user to register with empty fields
             */
            TextWatcher watcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    registerAction.setEnabled(s.toString().trim().length() > 0);
                }
                @Override
                public void afterTextChanged(Editable s) {
                }
            };
            /*We want to watch all EditText fields for input*/
            registerNameInput.addTextChangedListener(watcher);
            registerEmailInput.addTextChangedListener(watcher);
            registerUsernameInput.addTextChangedListener(watcher);
            registerPasswordInput.addTextChangedListener(watcher);
        }
    registerDialog.show();
    registerAction.setEnabled(false); //disabled by default

将您的代码更改为:

首先,您需要将 textwatcher 添加到特定的 EditText,而您没有这样做。尝试如下:

yourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });

然后,

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
     if (registerNameInput.getText().toString().length() > 0
                && registerEmailInput.getText().toString().length() > 0
                && registerUsernameInput.getText().toString().length() > 0
                && registerPasswordInput.getText().toString().length() > 0) {
                    registerAction.setEnabled(true);
     } else {
         registerAction.setEnabled(false);
     }
}

你可以这样做,但是不要复制我没有检查代码。

public class MyTextWatcher implements TextWatcher {

    EditText editText, anotherEditText;
    View myView;

// Your constructor
    public MyTextWatcher(EditText editText, EditText anotherEditText View myView) {
        this.editText = editText;
        this.myView = myView
        this.anotherEditText = anotherEditText
    }
 @Override
   public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      }

 @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) { 
      if(anotherEditText.getText().toString().trim().lenght()>0){
           myView.setEnabled(s.toString().trim().length() > 0);
        }
   }

 @Override
   public void afterTextChanged(Editable s) {//something }

使用这个class

editText.addTextChangedListener(new MyTextWatcher(editText, editText2, myView));

只要对您的实施方式稍作改动,您就可以实现这一目标。您可以创建一种方法,而不是向 onTextChanged 添加逻辑,例如

private void validateInput(){
        boolean enableButton = true;
        if (registerNameInput.getText() == null || registerNameInput.getText().toString() == null || registerNameInput.getText().toString().trim().length() == 0){
            enableButton = false;
        }
        if (registerEmailInput.getText() == null || registerEmailInput.getText().toString() == null || registerEmailInput.getText().toString().trim().length() == 0){
            enableButton = false;
        }
if (registerUsernameInput.getText() == null || registerUsernameInput.getText().toString() == null || registerUsernameInput.getText().toString().trim().length() == 0){
            enableButton = false;
        }
if (registerPasswordInput.getText() == null || registerPasswordInput.getText().toString() == null || registerPasswordInput.getText().toString().trim().length() == 0){
            enableButton = false;
        }


        registerAction.setEnabled(enableButton);
    }

并在 afterTextChanged()

中调用了这个方法
TextWatcher watcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }
                @Override
                public void afterTextChanged(Editable s) {
                    validateInput();
                }
            };

逐一检查的好处是,例如,对于您想要额外检查的电子邮件输入,您可以添加

if (registerEmailInput.getText() == null || registerEmailInput.getText().toString() == null || registerEmailInput.getText().toString().trim().length() == 0){
                enableButton = false;
            }else{
            //put if block to check email input format is correct or not, if not then set enableButton = false;
}

如果您想对多个编辑文本使用单个 TextWatcher class,这仅在 TextChanged(Editable editable)

之后有效
public void afterTextChanged(Editable editable) {
        if (editable == editText1.getEditableText()) {
            // do something
        } else if (editable == editText2.getEditableText()) {
            // do something
        }
    }