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
}
})
}
}
首先用您想要的Drawable
装饰EditText
。
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
并进行检查。
试试这个:
myEditText.setError(null);
myEditText.setErrorEnabled(false);
myEditText.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context,R.drawable.icon), null, ContextCompat.getDrawable(context,R.drawable.icon), null);