Android SetCompoundDrawableSwithinInstructBounds工作不正常
我的电子邮件字段为Android SetCompoundDrawableSwithinInstructBounds工作不正常,android,android-edittext,Android,Android Edittext,我的电子邮件字段为EditText。当验证为真时,我试图在文本字段的末尾添加一个绿色的勾号图标;当验证为假时,我试图在文本字段的末尾添加一个绿色的勾号图标 下面是我现在正在使用的一段代码: email.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) {
EditText
。当验证为真时,我试图在文本字段的末尾添加一个绿色的勾号图标;当验证为假时,我试图在文本字段的末尾添加一个绿色的勾号图标
下面是我现在正在使用的一段代码:
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是有效的代码>,似乎没有设置图标,因为我看不到它。
但是当我将if条件
更改为false
,这意味着永远不会调用setError
,我可以看到日志和图标
有没有解释为什么我会看到这种奇怪的行为?我缺少什么?如果要设置图标,请尝试从xml中删除图标
并从代码中设置两个图像
由于某些原因,如果从xml设置图像,则图像不会刷新
和使用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
numTxt.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
} else {
numTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
}
我不确定这是否是一个错误,但我可以通过在分配新的可绘制文件之前先将可绘制文件设置为零(0)来解决这个问题
在您的情况下,您可以尝试以下操作:
Log.i("YaY","Email is valid!!!");
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);
我没有从xml设置任何图标。请正确解释您的答案。@Akeshwar,只需将xml转换为可绘制对象,然后将其传递给建议的参数。是的,看起来它们有一些bug。您需要先进行初始化集(就像处理零一样),然后再进行实值集。那就行了。疯子