Android 如何通过在单个编辑文本中使用文本监视程序检查所有密码凭据(即一个大写字母、一个小写字母等)来自动选中复选框?
一个编辑文本用于密码我在编辑文本中输入文本,然后在下面我有四个复选框一个用于密码包含A-Z的复选框第二个用于密码包含A-Z的第三个用于0-9这样,当我输入密码时,它将自动检查例如,在我的密码中,我输入了大写字母,所以第一个复选框将自动选择等等。但是其仅适用于第一个字母,即选中复选框,但对于第二个字母,我输入其不适用 这是我的密码:Android 如何通过在单个编辑文本中使用文本监视程序检查所有密码凭据(即一个大写字母、一个小写字母等)来自动选中复选框?,android,textwatcher,Android,Textwatcher,一个编辑文本用于密码我在编辑文本中输入文本,然后在下面我有四个复选框一个用于密码包含A-Z的复选框第二个用于密码包含A-Z的第三个用于0-9这样,当我输入密码时,它将自动检查例如,在我的密码中,我输入了大写字母,所以第一个复选框将自动选择等等。但是其仅适用于第一个字母,即选中复选框,但对于第二个字母,我输入其不适用 这是我的密码: pwd.addTextChangedListener(new TextWatcher() { CheckBox checkBox1 = (CheckB
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";
}
pwd.addTextChangedListener(新的TextWatcher(){
复选框checkBox1=(复选框)findViewById(R.id.checkfeedback1);
复选框checkBox2=(复选框)findViewById(R.id.checkfeedback2);
复选框checkBox3=(复选框)findViewById(R.id.checkfeedback3);
public void onTextChanged(字符序列、int start、int before、int count){
字符串str=pwd.getText().toString();
如果(str.length()>0&&str.length()这里给你一些提示
1不要在addTextChangedListener
中使用pwd.getText().toString()
,而不要像下面的代码那样使用s
2不要在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+.*");
}
});
您是否已将所有pwd.getText().toString()更改为s?我现在已经尝试过这种方法,但它仍然不起作用。编辑文本中的第一个字母只调用一次onTextChanged(),而同一编辑文本中的第二个字母不起作用。String str=s.toString();if(str.length()>0&&str.length()一个编辑文本用于密码我在编辑文本中输入文本,然后在下面我有四个复选框一个用于密码的复选框包含A-Z第二个用于密码的复选框包含A-Z第三个用于0-9这样,当我输入密码时,它将自动检查,例如,在我的密码中,我输入了大写字母,所以第一个复选框将自动选择等等。但是它是w仅为第一个字母工作,即复选框已选中,但为第二个字母输入的代码无效。@pratival我为您编写新代码。请使用此方法检查我编辑的代码