Ake*_*Jha 3 validation android android-edittext
setError
如果EditText的验证返回false,我们有方法在框的末尾设置带有红色感叹号的错误消息:
我想设置一个绿色刻度符号,就像我的验证返回true时盒子末尾的红色感叹号一样:
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?
}
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
编辑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);
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
虽然我可以在我的日志中看到,但我看yay: Email is valid!!!
不到最后的验证符号EditText
.
但是,令我惊讶的是,当我将if
语句更改为always时false
,我可以看到带有log语句的符号.
有关为什么会发生这种情况的任何解释?
您可以drawableRight
在验证确认时显示.
password.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.rightImage, 0);
Run Code Online (Sandbox Code Playgroud)
验证为false时将其设置恢复正常.
password.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
我刚刚在我自己的项目中测试了它 setError(CharSequence error, Drawable icon)
获取一个新图标:
转到我的drawable文件夹
添加一个新的矢量资产.
我选择了"材料图标"并浏览,
然后选择了ic_done_24pp.
颜色:
接下来,我进入xml并通过更改fillcolor使其变为绿色:
android:fillColor="#FF00FF00"
3:更改代码:
Drawable myIcon = getResources().getDrawable(R.drawable.ic_done_24dp);
myIcon.setBounds(0, 0, myIcon.getIntrinsicWidth(), myIcon.getIntrinsicHeight());
mPasswordView.setError("Good", myIcon);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3978 次 |
最近记录: |