EDIT 我刚刚尝试了一个没有 TextInputLayout
的 EditText
并且它按预期工作。所以问题一定出在 TextInputLayout
的新变化上。
我已经使用自定义 EditText
类作为 TextInputLayout
的子级大约一个月了。当用户键入时,x
将出现在 drawableRight
字段中。我已经成功地显示了 drawableLeft
、drawableTop
和 drawableBottom
的图像,但是设置 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>
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]);
最佳答案
2016 年 9 月 14 日更新
支持库 24.2.1
的新版本已发布,此问题已标记为已修复。根据变更日志
Fixed issues:
TextInputLayout overrides right compound drawable. (AOSP issue 220728)
原始答案
警告 1 这个答案将破坏这个新的密码可见性切换功能。
警告 2 此答案可能会在更新支持库后导致意外行为(假设他们会解决此问题)。
看起来 TextInputLayout
把事情搞砸了,特别是 updatePasswordToggleView
方法中的这些行。
final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(mEditText);
TextViewCompat.setCompoundDrawablesRelative(mEditText, compounds[0], compounds[1], mPasswordToggleDummyDrawable, compounds[2]);
如您所见,它将 mPasswordToggleDummyDrawable
设置为 right
可绘制对象,然后设置 compounds[2]
(这是您的自定义可绘制对象,您想成为其中之一)作为可绘制的 bottom
。
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);
}
}
然后像这样使用它
<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>
(不要忘记更改包名称)
如您所见,在 TextInputLayout
将自定义可绘制对象设置为底部可绘制对象后,我们将其设置为右侧。
关于android - 新的 Password Visibility Toggle 是否破坏了 EditTexts 的现有 drawableRight?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39132192/