复合Drawable和setError有问题

复合Drawable和setError有问题

本文介绍了复合Drawable和setError有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用该库:

https://github.com/lisawray/passwordview

开启/关闭密码.我还使用setError密码为假时.它从一开始就有效.我输入了错误的密码.更改为正确的密码,在EditText中.尝试切换可绘制的眼睛",不再切换.密码文本可以正确切换,但不能使用setCompoundDrawablesWithIntrinsicBounds(...)

to toggle password on/off. Im also using setError when the password is false.It works from start. I enter a false password. Change to correct password in the EditText. Trying to toggle the drawable "eye", does not toggle anymore. The password text toggles correctly, but not the drawable set with setCompoundDrawablesWithIntrinsicBounds(...)

注意输入错误的密码:

右侧的小方块不再切换了(密码文本可以切换并起作用):

The right drawble does not toggle anymore (the password text toggles and works):

切换,相同的右侧可绘制对象:

Toggled, same right drawable:

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

I set the error in Activity (not the custom View):

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

拨动开关只具有一个可绘制状态,不会改变自身,仅改变文本,失去另一个可绘制状态.有什么想法可以调试/解决这个问题吗?

The toggle eye only have one drawable state, does not change itself, just the text, loosing the other drawable. Any ideas how to debug/solve this?

推荐答案

如果在自定义EditText实现中重写setError()方法并在调用.另外,不要忘记稍后再设置.

You can make this work if you override the setError() method in your custom EditText implementation and remove the compound drawable on the right, before calling super.setError(). Also don't forget to set it back afterwards.

示例:

@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]);
}

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

This way the right drawable will still be able to toggle after the error disappears.

这篇关于复合Drawable和setError有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:22