那里有一个密码编辑文本,我在编辑文本内输入文本,然后在下面有四个复选框,其中一个复选框包含AZ的密码第二个复选框包含0-9的az第三密码,这样我输入密码时它将自动检查,例如,在我的密码中,我输入了大写字母,因此第一个复选框会自动选择,依此类推。但是它仅对第一个字母有效,即选中了复选框,对于第二个字母,我输入了它不起作用
这是我的代码:
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";
}
答案 0 :(得分:1)
给您一些提示
1 请勿在{{1}}中使用pwd.getText().toString()
,而不要像下面的代码那样使用addTextChangedListener
2 不要在s
中初始化您的CheckBox,而不要在addTextChangedListener
中初始化该复选框
3 使用不同的方法进行验证
4 不要使用checkbox1,checkbox2,...而是使用checkboxLower,checkboxUpper,...
因此您的代码必须喜欢
onCreate()