Has the new Password Visibility Toggle broken existing drawableRight for EditTexts?

后端 未结 1 1239
名媛妹妹
名媛妹妹 2021-02-20 12:01

EDIT I just tried an EditText without a TextInputLayout and it works as expected. So the problem must be with new changes in the

1条回答
  •  盖世英雄少女心
    2021-02-20 12:40

    UPDATE 14 SEP 2016

    A new version of support library 24.2.1 is out and this issue is marked as fixed. According to changelog

    Fixed issues:

    TextInputLayout overrides right compound drawable. (AOSP issue 220728)

    Original answer

    Warning 1 This answer will break this new password visibility toggle feature.

    Warning 2 This answer may cause an unexpected behaviour after updating support lib (assuming that they will fix this issue).

    Looks like TextInputLayout screws things up here, specifically these lines from updatePasswordToggleView method.

    final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(mEditText);
    TextViewCompat.setCompoundDrawablesRelative(mEditText, compounds[0], compounds[1], mPasswordToggleDummyDrawable, compounds[2]);
    

    As you can see it sets mPasswordToggleDummyDrawable as a right drawable and then sets compounds[2] (which is your custom drawable that you want to be one the right) as a bottom drawable.

    updatePasswordToggleView method is called in onMeasure method. Possible workaround is to create a custom TextInputEditText and override it's onMeasure method. Let's call it PassFixTextInputEditText

    public class PassFixTextInputEditText extends TextInputEditText {
    
        public PassFixTextInputEditText(final Context context) {
            super(context);
        }
    
        public PassFixTextInputEditText(final Context context, final AttributeSet attrs) {
            super(context, attrs);
        }
    
        public PassFixTextInputEditText(final Context context, final AttributeSet attrs, final int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            Drawable[] drawables = getCompoundDrawables();
            setCompoundDrawables(drawables[0], drawables[1], drawables[3], null);
        }
    }
    

    and use it like this

    
    
        
    
    

    (don't forget to change the package name)

    As you can see, after TextInputLayout sets your custom drawable as bottom drawable we set it as a right one.

    0 讨论(0)
提交回复
热议问题