我希望在用户输入密钥时显示文本视图,这是我的代码
TextView mustContain, minChar, alphaNumeric;
这些是必须根据特定条件出现的3种不同的TextViews
。
minChar
,
如果用户键入密码而没有大写或小写字符,则应显示mustContain
只要用户没有输入至少一个数字
alphaNumeric
public class AccountDetails extends AppCompatActivity implements TextWatcher
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
final String validatePassword = password.getText().toString();
if (validatePassword.length()<8 || validatePassword.length()>24){
minChar.setText("minimum 8 characters, up to 24 characters");
minChar.setTextColor(Integer.parseInt("#ff0000"));
} else if (validatePassword.contains("^(?=.*?[A-Z])(?=.*?[a-z]).{8,}$")){
mustContain.setText("at least one upper-case and lower-case");
mustContain.setTextColor(Integer.parseInt("#ff0000"));
} else if (validatePassword.contains("(?=.*?[0-9])")){
alphaNumeric.setText("contain alphanumeric");
alphaNumeric.setTextColor(Integer.parseInt("#ff0000"));
} else {
minChar.setText("");
mustContain.setText("");
alphaNumeric.setText("");
}
}
@Override
public void afterTextChanged(Editable editable) {
final String validatePassword = password.getText().toString();
if (validatePassword.length()<8 || validatePassword.length()>24){
minChar.setText("minimum 8 characters, up to 24 characters");
minChar.setTextColor(Integer.parseInt("#ff0000"));
} else if (validatePassword.contains("^(?=.*?[A-Z])(?=.*?[a-z]).{8,}$")){
mustContain.setText("at least one upper-case and lower-case");
mustContain.setTextColor(Integer.parseInt("#ff0000"));
} else if (validatePassword.contains("(?=.*?[0-9])")){
alphaNumeric.setText("contain alphanumeric");
alphaNumeric.setTextColor(Integer.parseInt("#ff0000"));
} else {
minChar.setText("");
mustContain.setText("");
alphaNumeric.setText("");
}
}
}
答案 0 :(得分:1)
只需使用setVisibility()函数即可。将textViews的可见性初始化为false,并在完成编码时将可见性设置为true。例如:
minChar.setVisibility(View.INVISIBLE);
if (validatePassword.length()<8 || validatePassword.length()>24){
minChar.setText("minimum 8 characters, up to 24 characters");
minChar.setTextColor(Integer.parseInt("#ff0000"));
minChar.setVisibility(View.VISIBLE);
}