如何在单个编辑文本中使用 Text Watcher 通过检查所有密码凭据(即一个大写字母、一个小写字母等)来自动选中 select 复选框?

How to auto select checkbox by checking all password credentials(i.e one uppercase,one lowercase and so on) using Text Watcher in single Edit text?

密码有一个编辑文本在那里我在编辑文本中输入文本然后在下面我有四个复选框第一个包含 A-Z 的密码复选框第二个包含 a-z 第三个用于 0-9 这样当我输入密码时将自动检查例如在我的密码中我输入了大写字母所以第一个复选框自动 select 所以 on.But 它只对第一个字母起作用,即复选框是 selected 但第二个字母我输入它不工作

这是我的代码:

pwd.addTextChangedListener(new TextWatcher() {
        CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkfeedback1);
        CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkfeedback2);
        CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkfeedback3);
        public void onTextChanged(CharSequence s, int start, int before,int count) {
            String str=pwd.getText().toString();
            if (str.length()>0 && str.length()<5) {
                if (isValidPassword(pwd.getText().toString()).equals("first")) {
                    checkBox1.setChecked(true);
                    Toast.makeText(getApplicationContext(), "Password is valid", Toast.LENGTH_SHORT).show();
                } else {
                    checkBox1.setChecked(false);
                    Toast.makeText(getApplicationContext(), "Phone number or Password is not valid", Toast.LENGTH_SHORT).show();
                }
                if (isValidPassword(pwd.getText().toString()).equals("second")) {
                    checkBox2.setChecked(true);
                    Toast.makeText(getApplicationContext(), "Password is valid for second checkbox", Toast.LENGTH_SHORT).show();
                } else {
                    checkBox3.setChecked(false);
                    Toast.makeText(getApplicationContext(), "Phone number or Password is not valid", Toast.LENGTH_SHORT).show();
                }

                if (isValidPassword(pwd.getText().toString()).equals("third")) {
                    checkBox3.setChecked(true);
                    Toast.makeText(getApplicationContext(), "Password is valid for third checkbox", Toast.LENGTH_SHORT).show();
                } else {
                    checkBox3.setChecked(false);
                    Toast.makeText(getApplicationContext(), "Phone number or Password is not valid", Toast.LENGTH_SHORT).show();

                }
            } else {
                Toast.makeText(getApplicationContext(), "Please enter your mobile number", Toast.LENGTH_SHORT).show();
            }
        }
private String isValidPassword(String passwrd) {
    boolean check = false;
    if (Pattern.matches("[A-Z]+", passwrd)) {
        check = true;
        return "first";

    }
    if(Pattern.matches("[a-z]+", passwrd))
    {
        check = true;
        return "second";

    }
    if(Pattern.matches("[0-9]+", passwrd))
    {
        check = true;
        return "third";

    }
    else
    {
        check = false;

    }
    return "check";
}

这里给你一些提示

1 不要在 addTextChangedListener 中使用 pwd.getText().toString() 而不是像下面的代码那样使用 s

2 不要在 addTextChangedListener 中初始化 CheckBox 而不是在 onCreate()

中初始化

3 使用不同的验证方法

4 不要使用 checkbox1,checkbox2,... 而是使用 checkboxLower,checkboxUpper,...

所以你的代码必须像这样

   pws.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            checkBoxLower.setChecked(hasLowerCase(charSequence.toString()));
            checkBoxUpper.setChecked(hasUpperCase(charSequence.toString()));
            checkBoxNumber.setChecked(hasNumber(charSequence.toString()));
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }

        private boolean hasLowerCase(String s){
            return !s.equals(s.toUpperCase());
        }

        private boolean hasUpperCase(String s){
            return !s.equals(s.toLowerCase());
        }

        private boolean hasNumber(String s){
            return s.matches(".*\d+.*");
        }

    });