新的密码可见性切换是否破坏了EditTexts的现有drawableRight? [英] Has the new Password Visibility Toggle broken existing drawableRight for EditTexts?

本文介绍了新的密码可见性切换是否破坏了EditTexts的现有drawableRight?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑,我只是尝试了一个不带TextInputLayoutEditText,它可以正常工作.因此问题必须出在TextInputLayout中的新变化.

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

我已经使用自定义EditText类作为TextInputLayout的子级了大约一个月.当用户键入时,x将出现在drawableRight字段中.我已经成功显示了drawableLeftdrawableTopdrawableBottom的图像,但是设置drawableRight为我提供了空白. 注意:单击X应该可以正常工作的空白,将清除文本.

I have been using a custom EditText class as child of a TextInputLayout for around a month. When the user typed, an x would appear in the drawableRight field. I have successfully displayed images for drawableLeft, drawableTop, and drawableBottom, but setting drawableRight provides me with a blank. Note: Clicking the blank space where the X SHOULD be works as expected, the text is cleared.

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

This first picture is how it originally looked:

自升级到support-v4:24.2.0以来,此功能已被破坏.现在,它将"x"放置在应显示带有drawableBottom的可绘制集的位置.第二张图片显示了新的行为:

Ever since upgrading to support-v4:24.2.0 the functionality has been broken. It now places the "x" where a drawable set with drawableBottom should appear. This second picture shows the new behavior:

        <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的新版本已发布,此问题标记为已修复.根据changelog

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覆盖正确的复合可绘制对象. (AOSP问题220728)

TextInputLayout overrides right compound drawable. (AOSP issue 220728)

原始答案

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

Original answer

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

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

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

看起来像TextInputLayout一样,这里的东西特别是updatePasswordToggleView方法中的这些行.

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]);

如您所见,它会将mPasswordToggleDummyDrawable设置为right可绘制对象,然后将compounds[2](这是您希望自定义的自定义可绘制对象,您希望将其设置为右侧)作为bottom可绘制对象.

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方法.可能的解决方法是创建自定义TextInputEditText并覆盖其onMeasure方法.我们称之为PassFixTextInputEditText

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);
    }
}

并像这样使用它

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

(不要忘记更改程序包名称)

(don't forget to change the package name)

如您所见,在TextInputLayout将自定义可绘制对象设置为底部可绘制对象之后,我们将其设置为正确的对象.

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

这篇关于新的密码可见性切换是否破坏了EditTexts的现有drawableRight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆