如果 EditText 的验证返回 false,我们有 setError
方法用于在框的末尾设置带有红色感叹号的错误消息:
我想设置一个绿色刻度符号,就像我的验证返回真时框末尾的红色感叹号:
password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (password.getText().toString().length() < 6) {
password.setError("Password should be greater than 6 characters!");
}
else {
//validation is true, so what to put here?
}
}
}
});
}
编辑 1 看似不可能,但问题不止于此。我这样做了:
email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String mail = email.getText().toString();
if(!android.util.Patterns.EMAIL_ADDRESS.matcher(mail).matches()) {
email.setError("Please enter a valid email address");
}
else {
Log.i("yay!","Email is valid!!!");
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);
}
}
}
});
虽然我可以在我的日志中看到 yay: Email is valid!!!
,但我看不到 EditText
末尾的验证符号。
然而,令我惊讶的是,当我将 if
语句更改为始终 false
时,我可以看到带有日志语句的符号。
对为什么会发生这种情况有任何解释吗?
最佳答案
当验证为真时,您可以显示 drawableRight
。
password.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.rightImage, 0);
并在验证为假时将其恢复正常。
password.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
关于Android:EditText 中的验证符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847356/