微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 错误时不要更改TextInputLayout后台

我想将一个带有背景的EditText作为“普通”EditText,但是对TextInputEditText进行错误处理(错误消息出现在底部,而不是“!”drawable出现).

我有这样的事情:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:setError="@{viewmodel.error}">

    <android.support.design.widget.TextInputEditText

        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:background="@drawable/simple_edit_text_background"
        android:ellipsize="end"
        android:inputType="textMultiLine|textNoSuggestions"
        android:text="@={viewmodel.value}"

        style="@style/MyEditTextStyle" />

</android.support.design.widget.TextInputLayout>

但似乎当我在TextInputLayout上设置错误时,它将背景drawable(在普通的TextInputEditText中,下划线)更改为错误TextView的颜色.

这就是我的EditText的样子:

我们可以在以下方法中的TextInputLayout代码中看到它:

private void updateEditTextBackground() {
    if (mEditText == null) {
        return;
    }

    Drawable editTextBackground = mEditText.getBackground();
    if (editTextBackground == null) {
        return;
    }

    ensureBackgroundDrawableStateWorkaround();

    if (android.support.v7.widget.Drawableutils.canSafelyMutateDrawable(editTextBackground)) {
        editTextBackground = editTextBackground.mutate();
    }

    if (mErrorShown && mErrorView != null) {
        // Set a color filter of the error color
        editTextBackground.setColorFilter(
                AppCompatDrawableManager.getPorterDuffColorFilter(
                        mErrorView.getCurrentTextColor(),PorterDuff.Mode.SRC_IN));
    } else if (mCounterOverflowed && mCounterView != null) {
        // Set a color filter of the counter color
        editTextBackground.setColorFilter(
                AppCompatDrawableManager.getPorterDuffColorFilter(
                        mCounterView.getCurrentTextColor(),PorterDuff.Mode.SRC_IN));
    } else {
        // Else reset the color filter and refresh the drawable state so that the
        // normal tint is used
        DrawableCompat.clearColorFilter(editTextBackground);
        mEditText.refreshDrawableState();
    }
}

更新背景颜色的代码在这里

if (mErrorShown && mErrorView != null) {
    // Set a color filter of the error color
    editTextBackground.setColorFilter(
            AppCompatDrawableManager.getPorterDuffColorFilter(
                    mErrorView.getCurrentTextColor(),PorterDuff.Mode.SRC_IN));
}

因为这个方法是私有的,所以我无法覆盖它,因为我仍然希望我的错误TextView的颜色为红色,到目前为止我看不到任何解决方案.任何的想法?

一个解决方案可能是在调用setError之后将背景颜色重置为其认值,但是它们是否使用像onError这样的方法进行回调,一旦将错误设置为TextView / EditText就会触发?

解决方法

我设法通过覆盖TextInputLayout来解决这个问题,如下所示:
public class NoChangingBackgroundTextInputLayout extends TextInputLayout {
    public NoChangingBackgroundTextInputLayout(Context context) {
        super(context);
    }

    public NoChangingBackgroundTextInputLayout(Context context,AttributeSet attrs) {
        super(context,attrs);
    }

    public NoChangingBackgroundTextInputLayout(Context context,AttributeSet attrs,int defStyleAttr) {
        super(context,attrs,defStyleAttr);
    }

    @Override
    public void setError(@Nullable CharSequence error) {
        ColorFilter defaultColorFilter = getBackgroundDefaultColorFilter();
        super.setError(error);
        //Reset EditText's background color to default.
        updateBackgroundColorFilter(defaultColorFilter);
    }

    @Override
    protected void drawableStateChanged() {
        ColorFilter defaultColorFilter = getBackgroundDefaultColorFilter();
        super.drawableStateChanged();
        //Reset EditText's background color to default.
        updateBackgroundColorFilter(defaultColorFilter);
    }

    private void updateBackgroundColorFilter(ColorFilter colorFilter) {
        if(getEditText() != null && getEditText().getBackground() != null)
            getEditText().getBackground().setColorFilter(colorFilter);
    }

    @Nullable
    private ColorFilter getBackgroundDefaultColorFilter() {
        ColorFilter defaultColorFilter = null;
        if(getEditText() != null && getEditText().getBackground() != null)
            defaultColorFilter = DrawableCompat.getColorFilter(getEditText().getBackground());
        return defaultColorFilter;
    }
}

因此,我们可以看到它,它在调用setError之后将EditText的背景重置为其认颜色,但也在方法drawableStateChanged()中重置,因为在丢失/获得焦点的EditText时也设置了红色滤镜.

我不相信这是最好的解决方案,但如果我没有得到任何更好的解决方案,我会在此期间将其标记为已解决.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐