android TextInputLayout在将错误设置为null后更改EditText样式

android TextInputLayout在将错误设置为null后更改EditText样式,android,android-textinputlayout,Android,Android Textinputlayout,这是我第一次使用新的Android小部件TextInputLayout,它非常好,但我在使用setError方法时遇到了一些问题 这是我的xml <android.support.design.widget.TextInputLayout android:id="@+id/userData_txtNameWrapper" android:layout_width="match_parent" android:layout_height="wrap_content"

这是我第一次使用新的Android小部件TextInputLayout,它非常好,但我在使用setError方法时遇到了一些问题

这是我的xml

<android.support.design.widget.TextInputLayout
    android:id="@+id/userData_txtNameWrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColorHint="@color/light_gray"
    app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout">
    <EditText
        android:id="@+id/userData_txtName"
        style="@style/bold_textbox_style"
        android:layout_width="match_parent"
        android:layout_height="@dimen/textinut_height"
        android:layout_margin="5dp"
        android:hint="name"
        android:imeOptions="actionNext"
        android:inputType="text"
        android:paddingTop="10dp"
        android:textSize="@dimen/medium_text"/>
</android.support.design.widget.TextInputLayout>
整个EditText背景和提示文本颜色变为红色,从这里开始就可以了。问题是我什么时候跑步

setError("error message") 
setError(null) 
EditText的样式与原始样式完全不同

开始情况:

不集中 专注

之后
设置错误(“必填字段”)

之后
设置错误(null)

我做了很多研究,但是没有发现任何有用的东西,到底是什么问题

更新

在对
setError()
方法的android源代码进行调查时,我发现了这一点

public void setError(@Nullable CharSequence error) {
    if (!mErrorEnabled) {
        if (TextUtils.isEmpty(error)) {
            // If error isn't enabled, and the error is empty, just return
            return;
        }
        // Else, we'll assume that they want to enable the error functionality
        setErrorEnabled(true);
    }
    if (!TextUtils.isEmpty(error)) {
        ViewCompat.setAlpha(mErrorView, 0f);
        mErrorView.setText(error);
        ViewCompat.animate(mErrorView)
                .alpha(1f)
                .setDuration(ANIMATION_DURATION)
                .setInterpolator(AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR)
                .setListener(new ViewPropertyAnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(View view) {
                        view.setVisibility(VISIBLE);
                    }
                })
                .start();
        // Set the EditText's background tint to the error color
        mErrorShown = true;
        updateEditTextBackground();
        updateLabelVisibility(true);
    } else {
        if (mErrorView.getVisibility() == VISIBLE) {
            ViewCompat.animate(mErrorView)
                    .alpha(0f)
                    .setDuration(ANIMATION_DURATION)
                    .setInterpolator(AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR)
                    .setListener(new ViewPropertyAnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(View view) {
                            view.setVisibility(INVISIBLE);
                            updateLabelVisibility(true);
                        }
                    }).start();
            // Restore the 'original' tint, using colorControlNormal and colorControlActivated
            mErrorShown = false;
            updateEditTextBackground();
        }
    }


    private void updateEditTextBackground() {
        if (mErrorShown && mErrorView != null) {
            // Set the EditText's background tint to the error color
            ViewCompat.setBackgroundTintList(mEditText,
                    ColorStateList.valueOf(mErrorView.getCurrentTextColor()));
        } else if (mCounterOverflowed && mCounterView != null) {
            ViewCompat.setBackgroundTintList(mEditText,
                    ColorStateList.valueOf(mCounterView.getCurrentTextColor()));
        } else {
            final TintManager tintManager = TintManager.get(getContext());
            ViewCompat.setBackgroundTintList(mEditText,
                    tintManager.getTintList(R.drawable.abc_edit_text_material));
        }
    }
调试代码时,我发现在
updateEditttextBackground()
中执行的代码如下

final TintManager tintManager = TintManager.get(getContext());
ViewCompat.setBackgroundTintList(mEditText,
        tintManager.getTintList(R.drawable.abc_edit_text_material));
android似乎在随意地替换EditText的背景色。我试图用以下代码在我的可绘图文件夹中创建一个名为abc\u edit\u text\u material.xml的文件

<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:insetLeft="@dimen/abc_edit_text_inset_horizontal_material"
       android:insetRight="@dimen/abc_edit_text_inset_horizontal_material"
       android:insetTop="@dimen/abc_edit_text_inset_top_material"
       android:insetBottom="@dimen/abc_edit_text_inset_bottom_material">

    <selector>
        <item android:state_enabled="false" android:drawable="@color/white"/>
        <item android:state_pressed="false" android:state_focused="false" android:drawable="@color/white"/>
        <item android:drawable="@color/white"/>
    </selector>

</inset>

我快疯了

设置文本输入的样式

<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorPrimaryDark</item>
        <item name="android:textColorHint">@color/colorPrimaryDark</item>
        <item name="android:textSize">14sp</item>
        <item name="colorAccent">@color/colorPrimaryDark</item>
        <item name="colorControlNormal">@color/colorPrimaryDark</item>
        <item name="colorControlActivated">@color/colorPrimaryDark</item>
    </style>
我的xml在哪里

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
    <stroke android:width="2dp" android:color="#ff207d94" />
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="5dp" />
    <solid android:color="#ffffffff" />
</shape>

将错误设置为null后,只需将颜色更改回所需的颜色即可。比如:

yourEditText.setError(null);
yourEditText.getBackground().mutate().setColorFilter(
            ContextCompat.getColor(getContext() , R.color.somecolor),
            PorterDuff.Mode.SRC_ATOP);

这是
support:design:23.2.0
中的一个bug(可能是较旧的版本),据报告是一个问题
23.3.0

中,我遇到了类似的问题,并找到了一个简单的解决方案。如果我们在
TextInputLayout
内将自定义背景可绘制/颜色设置为
EditText
,则会出现此问题。解决方法是将
TextInputLayout
子类化,覆盖
setError()
drawableStateChanged()
方法,并再次将自定义的drawable/color设置为
EditText的
背景。例如,我的
EditText的
背景有一个圆角可绘制集,下面是我的子类

public class RoundedBorderedTextInputLayout extends TextInputLayout {
    private Context context;

    public RoundedBorderedTextInputLayout(Context context) {
        super(context);
        this.context = context;
    }

    public RoundedBorderedTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public RoundedBorderedTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();

        EditText editText = getEditText();
        if(editText != null) {
            editText.setBackground(ContextCompat.getDrawable(this.context, R.drawable.custom_rounded_edittext));
        }
    }

    @Override
    public void setError(@Nullable final CharSequence error) {
        super.setError(error);

        EditText editText = getEditText();
        if(editText != null) {
            editText.setBackground(ContextCompat.getDrawable(this.context, R.drawable.custom_rounded_edittext));
        }
    }
}
然后在xml中使用自定义类

<com.example.RoundedBorderedTextInputLayout
                android:id="@+id/text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/edittext"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"/>

  </com.example.RoundedBorderedTextInputLayout>


希望这有帮助。快乐的Android编码:)

我有一个技巧可以简单地解决这个问题:

1,新的一个类扩展android.support.design.widget.textInputItemText; 2、覆盖getBackground()方法,使其返回null


由于TextInputLayout中的updateEditTextBackground()方法将判断editText的背景可绘制值是否为null,并且现在总是返回null,因此结果是editText的背景不会因错误文本颜色而改变。

仅获得了该方法中setError的解决方案

i、 e.我使用了自定义文本输入布局

<com.adminworksite.Design.NoChangingBackgroundTextInputLayout
                        android:id="@+id/city_til_of_job_create"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tv_placeholder_five"
                        android:layout_marginLeft="@dimen/left_right"
                        android:layout_marginRight="@dimen/left_right"
                        app:counterEnabled="true"
                        app:counterMaxLength="35">

                        <EditText
                            android:id="@+id/city_et_of_job_create"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@drawable/edittext_background"
                            android:inputType="text"
                            android:paddingLeft="@dimen/left_right"
                            android:paddingRight="@dimen/left_right" />
                    </com.adminworksite.Design.NoChangingBackgroundTextInputLayout>
这是我的自定义文本输入布局

<com.adminworksite.Design.NoChangingBackgroundTextInputLayout
                        android:id="@+id/city_til_of_job_create"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tv_placeholder_five"
                        android:layout_marginLeft="@dimen/left_right"
                        android:layout_marginRight="@dimen/left_right"
                        app:counterEnabled="true"
                        app:counterMaxLength="35">

                        <EditText
                            android:id="@+id/city_et_of_job_create"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@drawable/edittext_background"
                            android:inputType="text"
                            android:paddingLeft="@dimen/left_right"
                            android:paddingRight="@dimen/left_right" />
                    </com.adminworksite.Design.NoChangingBackgroundTextInputLayout>
当超过限制时,颜色仍然显示,并且颜色也更改为粉红色

所以我为它构建了一个解决方案,使用addTextChangedListener编辑文本并 按代码设置背景

代码片段

在最后的验证方法中,我再次使用了setbackground来设置我的editText

代码段:


setError(null)=>clearError()?你确定这样的方法存在吗?setErrorEnabled(false)?请你在使用setError时完成代码。让我们看看。已经有了与textColor、textColorHint和textSIze类似的样式,添加了colorAccent、colorControlNormal和colorControlActivated,分别用蓝色值化,红色和黄色,但没有任何变化@颜色/电绿色@颜色/浅灰色14sp@颜色/红色@颜色/黄色@颜色/蓝色不…不是文本输入布局样式…我要的是您的编辑文本样式…粗体文本框样式5dp 5dp中心样式垂直@颜色/白色@Ajinkya抱歉,刚刚修复了链接问题是在编译'com.android.support:design:23.3.0'中修复了此问题。唯一帮助我的是将此anser与设置背景的anser相结合:yourEditText.getBackground().mutate().setColorFilter(ContextCompat.getColor(getContext(),R.color.somecolor),PorterDuff.Mode.SRC_);
<com.adminworksite.Design.NoChangingBackgroundTextInputLayout
                        android:id="@+id/city_til_of_job_create"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tv_placeholder_five"
                        android:layout_marginLeft="@dimen/left_right"
                        android:layout_marginRight="@dimen/left_right"
                        app:counterEnabled="true"
                        app:counterMaxLength="35">

                        <EditText
                            android:id="@+id/city_et_of_job_create"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@drawable/edittext_background"
                            android:inputType="text"
                            android:paddingLeft="@dimen/left_right"
                            android:paddingRight="@dimen/left_right" />
                    </com.adminworksite.Design.NoChangingBackgroundTextInputLayout>
app:counterEnabled="true"
app:counterMaxLength="35"
editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    textInputLayout.setErrorEnabled(true);
                    textInputLayout.setError("Input should not be empty!");
                } else {
                    textInputLayout.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                setBackgroundToEt(editText);
            }
        });


private void setBackgroundToEt(EditText editText) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            editText.setBackground(getResources().getDrawable(R.drawable.edittext_background));
        }
    }
private boolean validateInputs() {
//validation code...
setBackgroundToEt(editText);
}