【问题标题】:Compound Drawable and setError problematic复合 Drawable 和 setError 有问题
【发布时间】:2016-04-07 12:53:32
【问题描述】:

我正在使用这个库:

https://github.com/lisawray/passwordview

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

注意输入错误密码:

右drawble不再切换(密码文本切换并且有效):

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

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

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

toggle eye只有一个drawable状态,不会改变自己,只是文本,失去另一个drawable。任何想法如何调试/解决这个问题?

【问题讨论】:

标签: android android-custom-view


【解决方案1】:

如果您在自定义EditText 实现中覆盖setError() 方法并在调用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]);
}

这样在错误消失后,正确的drawable仍然可以切换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多