新的密码可见性切换是否破坏了EditTexts的现有drawableRight?

Jui*_*iCe 10 android android-edittext android-support-library android-drawable android-textinputlayout

编辑我刚试了一个EditText没有a TextInputLayout,它按预期工作.所以问题必然是新的变化TextInputLayout.

我一直在使用自定义EditText类作为TextInputLayout一个月的孩子.当用户键入时,x会出现在drawableRight字段中.我已经成功地显示图像了drawableLeft,drawableTopdrawableBottom,但设置drawableRight为我提供了一个空白.注意:单击X应该按预期工作的空白区域,文本将被清除.

第一张照片是它最初的样子:

在此输入图像描述

自从升级到support-v4:24.2.0功能已被打破.它现在将"x"放置在drawableBottom应该出现的可绘制集合的位置.第二张图片显示了新的行为:

在此输入图像描述

XML代码

        <android.support.design.widget.TextInputLayout
            android:id="@+id/til_delivery_info_state"
            android:hint="@string/state_hint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/large_margin"
            android:layout_marginRight="@dimen/large_margin">
            <com.example.ui.edittexts.ClearableEditText
                android:id="@+id/et_state"
                android:inputType="textCapWords|text|textNoSuggestions"
                android:nextFocusDown="@+id/et_di_zip_code"
                android:text="@={deliveryViewModel.state}"
                android:gravity="center_vertical|left"
                android:singleLine="true"
                android:textSize="@dimen/text_size"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

Java的

    final Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_clear_text_gray_x);
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    mClearTextIcon.setBounds(0, 0, mClearTextIcon.getIntrinsicWidth(), mClearTextIcon.getIntrinsicHeight());
    mClearTextIcon.setVisible(true, false);
    final Drawable[] compoundDrawables = getCompoundDrawables();
    setCompoundDrawablesWithIntrinsicBounds(
            compoundDrawables[0],
            compoundDrawables[1],
            visible ? mClearTextIcon : null,
            compoundDrawables[3]);
Run Code Online (Sandbox Code Playgroud)

kam*_*ych 9

2016年9月14日更新

已经出现了新版本的支持库24.2.1,此问题已标记为已修复.根据changelog

修复问题:

TextInputLayout覆盖右复合drawable.(AOSP问题220728)

原始答案

警告1 此答案将破坏此新密码可见性切换功能.

警告2 更新支持lib后,此答案可能会导致意外行为(假设它们将解决此问题).

看起来像TextInputLayout这里的东西,特别是updatePasswordToggleView方法的这些线.

final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(mEditText);
TextViewCompat.setCompoundDrawablesRelative(mEditText, compounds[0], compounds[1], mPasswordToggleDummyDrawable, compounds[2]);
Run Code Online (Sandbox Code Playgroud)

你可以看到它设置mPasswordToggleDummyDrawablerightdrawable,然后设置compounds[2](这是你想要成为一个右边的自定义drawable)作为bottomdrawable.

updatePasswordToggleView方法在onMeasure方法中调用.可能的解决方法是创建自定义TextInputEditText并覆盖它的onMeasure方法.我们称之为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);
    }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="true">

    <com.kamilzych.temp.PassFixTextInputEditText
        android:id="@+id/textInputEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="23"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

(别忘了更改包名称)

如您所见,在TextInputLayout将自定义drawable设置为bottom drawable之后,我们将其设置为正确的drawable.