TextInputLayout提示焦点上的文本颜色发生变化

时间:2016-09-10 14:26:09

标签: android android-layout android-fragments

最初我将文本提示颜色设置为灰色...如果验证失败然后将提示颜色更改为红色..但是如果我移动到另一个EditText(每当丢失焦点)..颜色显示默认..

以下是我在布局中使用的代码

                <android.support.design.widget.TextInputLayout
                android:id="@+id/tip_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/img_email"
                android:hint="@string/email"
                app:hintTextAppearance="@style/EditTextHint">

                <EditText
                    android:id="@+id/et_email"
                    style="@style/EditTextWithBlackMediumSizeWithoutLeftAndTopPadding10"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:inputType="textEmailAddress"
                    android:textColorHint="@color/label_input_color" />

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

我用来修改颜色的样式

 <style name="EditTextHint" parent="MyMaterialTheme">
    <item name="android:textColor">@color/label_input_color</item>
    <item name="android:textSize">@dimen/font_mini</item>
    <item name="android:textAllCaps">@bool/fletTextcaps</item>
</style>

<style name="EditTextHintError" parent="MyMaterialTheme">
    <item name="android:textColor">@color/tab_selected_text_color</item>
    <item name="android:textSize">@dimen/font_mini</item>
    <item name="android:textAllCaps">@bool/fletTextcaps</item>
</style>

我用来改变颜色的Java代码是

tip_email.setHintTextAppearance(R.style.EditTextHintError);

tip_email.setHintTextAppearance(R.style.EditTextHintError);

附加截图

Starting Screen Email validation failed and color changed to red

我需要颜色相同甚至焦点已经改变.. 请建议....

1 个答案:

答案 0 :(得分:1)

这似乎是TextInputLayout课程中的一个错误。当您通过调用setHintTextAppearance更新HintTextAppearance时,它只会更改mFocusedTextColor而不会更改mDefaultTextColor

您可以查看源代码:TextInputLayout source

使用反射解决方案,同时更改颜色:

public void setInputTextLayoutColor(TextInputLayout til, @ColorInt int color) {
    try {
        final Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
        fDefaultTextColor.setAccessible(true);
        fDefaultTextColor.set(til, new ColorStateList(new int[][]{{0}}, new int[]{color}));

        final Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
        fFocusedTextColor.setAccessible(true);
        fFocusedTextColor.set(til, new ColorStateList(new int[][]{{0}}, new int[]{color}));
    } catch (Exception e) {
        e.printStackTrace();
    }
}