showError之后不会更新EditText右drawable

时间:2019-11-18 09:04:59

标签: android android-edittext android-appcompat android-textinputedittext

调用EditText方法后,在drawable右边的

showError不会更新。我试图将setError设置为null,将正确的drawable设置为null,但没有任何帮助。

Drawable right = DrawableUtils.getDrawable(context,R.drawable.eye_look);
right.setBounds(new Rect(0, 0, rigth.getIntrinsicWidth(), right.getIntrinsicHeight()));
myEditText.setError(null, null);
myEditText.setCompoundDrawablesWithIntrinsicBounds(DrawableUtils.getDrawable(context,R.drawable.pass_look), null, right, null);

有什么想法吗?

检查代码示例:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    vEditText.error = "Some error"
    vButton.setOnClickListener {
        vEditText.error = null
    }

    vEditText.setOnTouchListener(object : OnTouchListener {
        override fun onTouch(p0: View?, event: MotionEvent?): Boolean {
            val DRAWABLE_LEFT = 0
            val DRAWABLE_TOP = 1
            val DRAWABLE_RIGHT = 2
            val DRAWABLE_BOTTOM = 3

            if (event?.action == MotionEvent.ACTION_UP) {
                if (event.rawX >= (vEditText.right - vEditText.compoundDrawables[DRAWABLE_RIGHT].bounds.width())) {
                    vEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(vEditText.context,android.R.drawable.btn_star_big_on), null)
                    vEditText.error = null
                    return true
                }
            }
            return false
        }
    })
}

}

2 个答案:

答案 0 :(得分:0)

尝试一下:

myEditText.setError(null);
myEditText.setErrorEnabled(false);
myEditText.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context,R.drawable.pass_look), null, ContextCompat.getDrawable(context,R.drawable.eye_look), null);

答案 1 :(得分:0)

首先用所需的EditText装饰Drawable

Drawable right = DrawableUtils.getDrawable(context,R.drawable.eye_look);
right.setBounds(new Rect(0, 0, rigth.getIntrinsicWidth(), right.getIntrinsicHeight()));
myEditText.setCompoundDrawablesWithIntrinsicBounds(DrawableUtils.getDrawable(context,R.drawable.pass_look), null, right, null);

...

现在只要需要显示错误,只需使用

myEditText.setError("Your error message");

并隐藏如下所示的可绘制错误

myEditText.setError(null);

实际上隐藏了错误可绘制对象并显示了可绘制对象。无需再做任何事情。

///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// /////////

我已经更新了您的代码,请立即检查

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    //This is important to set here
    vEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(vEditText.context,android.R.drawable.btn_star_big_on), null)

    vEditText.error = "Some error"
    vButton.setOnClickListener {
        vEditText.error = null
    }
}

现在在vEditText上键入内容,或单击vButton并检查。