问题
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