Compound Drawable and setError problematic

可紊 提交于 2019-12-13 07:46:25

问题


Im using this library:

https://github.com/lisawray/passwordview

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(...)

Look after enter wrong password:

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?


回答1:


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.

Example:

@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.



来源:https://stackoverflow.com/questions/36476806/compound-drawable-and-seterror-problematic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!