setCompoundDrawablesWithIntrinsicBounds无法正常工作

Ake*_*Jha 4 android android-edittext

我有一个电子邮件字段EditText.我正在尝试在验证为真时在文本字段的末尾添加绿色勾号图标,并且setError当它为假时.

这是我正在使用的代码片段:

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!!!,但似乎没有设置图标,因为我看不到它.但是当我改变if-conditionto false,这意味着setError永远不会被调用时,我可以看到日志和图标.

为什么我看到这种奇怪的行为的任何解释?我错过了什么?

Hal*_*a.M 10

尝试从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);
    }
Run Code Online (Sandbox Code Playgroud)