复合Drawable和setError有问题

时间:2016-04-07 12:53:33

标签: android android-custom-view

我正在使用这个库:

https://github.com/lisawray/passwordview

打开/关闭密码。当密码为false时,我也使用setError。 它从一开始就有用。我输入了一个假密码。在EditText中更改为正确的密码。试图切换可绘制的“眼睛”,不再切换。密码文本正确切换,但不能切换setCompoundDrawablesWithIntrinsicBounds(...)的可绘制集

请输入错误的密码:

enter image description here

右边的抽屉不再切换(密码文本切换和工作):

enter image description here

切换,同样正确的绘制:

enter image description here

我在Activity中设置了错误(不是自定义视图):

loginEditText.setError(getText(R.string.wrong_pwd));

切换眼睛只有一个可绘制状态,不会改变自身,只改变文本,丢失另一个可绘制状态。有任何想法如何调试/解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果在自定义setError()实现中覆盖EditText方法并在调用super.setError()之前删除右侧的可绘制复合内容,则可以使此工作生效。另外,不要忘记稍后再设置。

示例:

@Override
public void setError(CharSequence error, Drawable icon) {
    Drawable[] drawables = getCompoundDrawables();
    setCompoundDrawables(drawables[0], drawables[1], null, drawables[3]);

    super.setError(error, icon);

    setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
}

这样,在错误消失后,右侧可绘制对象仍将能够切换。

相关问题