首页 > 解决方案 > 如何通过在单个编辑文本中使用文本观察器检查所有密码凭据(即一个大写,一个小写等)来自动选择复选框?

问题描述

有一个编辑文本用于密码,我在编辑文本中输入文本,然后在下面我有四个复选框,一个用于密码的复选框,其中包含 AZ 第二个用于包含 az 第三个,用于 0-9,因此当我输入密码时,它会自动检查,例如 In我的密码我输入了大写字母,所以第一个复选框会自动选择等等。但它只适用于第一个字母,即复选框被选中,但对于我输入的第二个字母它不起作用

这是我的代码:

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";
}

标签: androidtextwatcher

解决方案


这里给你一些提示

1不要使用pwd.getText().toString()inaddTextChangedListener而不是使用s下面的代码

2不要初始化 CheckBox,addTextChangedListener而不是初始化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+.*");
        }

    });

推荐阅读