如何在将错误设置为特定的textinputlayout(其中包含edittext)时不更改标签的颜色?

时间:2018-11-17 06:04:33

标签: android xml android-layout

我在其中使用Textinputlayout和edittext,问题是在验证时将错误设置为textinputlayout时,它会将提示标签的颜色也更改为红色。当出现错误时,我想用另一种颜色代替红色,只有下划线颜色和错误文本应该变成红色。

enter image description here

上面显示的图片是问题,我不希望电子邮件标签为红色。

下面是我的代码。

xml文件

     <android.support.design.widget.TextInputLayout
            android:layout_marginTop="30dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="15dp"
 app:errorTextAppearance="@style/error_appearance"
        app:hintTextAppearance="@style/TextLabel1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et1"
            android:padding="0dp">

        <EditText
            android:id="@+id/email"
            android:hint="Email"
            android:textColorHint="#3F4B5B"
            android:maxLines="1"
            android:inputType="text"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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


#java file


#Declarations
 private TextInputLayout emailerror;
 private EditText Email;

#On create 
    Email=(EditText) findViewById(R.id.email);
     emailerror=(TextInputLayout) findViewById(R.id.et1);

#Inside validation method
 emailerror.setError("Enter a valid email");

Style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
         TextInputLayout text color
        <item name="colorControlNormal">#e0e0e0</item>
        <item name="colorControlActivated">#52AF44</item>
        <item name="android:textColorHint">#3F4B5B</item>
        <item name="android:windowFullscreen">true</item>
    </style>


<!--    <style name="error" parent="@android:style/TextAppearance">
        <item name="android:textColor">#ff0000</item> &lt;!&ndash;apply the color you wat here &ndash;&gt;
        <item name="android:textColorHint">#3E4A58</item>
       &lt;!&ndash; <item name="android:textColorHint">#3a52a6</item>&ndash;&gt;
       &lt;!&ndash; <item name="android:textSize">12dp</item>&ndash;&gt;
    </style>-->

    <!--<style name="Widget.Design.TextInputLayout" parent="AppTheme">
        <item name="hintTextAppearance">@style/TextLabel1</item>
        <item name="errorTextAppearance">@style/error_appearance</item>
        <item name="counterTextAppearance">@style/TextAppearance.Design.Counter</item>
        <item name="counterOverflowTextAppearance">@style/TextAppearance.Design.Counter.Overflow</item>
    </style>-->

    <style name="error_appearance" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorAccent</item>
    </style>

    <style name="TextLabel1" parent="TextAppearance.Design.Hint">
        <item name="android:textColor">@color/colorPrimaryDark</item>
        <item name="android:textSize">16sp</item>
    </style>

    <style name="ToolbarColoredBackArrow" parent="AppTheme">
        <item name="android:textColorSecondary">@color/white</item>
    </style>

    <style name="EditTextHint" parent="Theme.AppCompat">
        <!--<item name="colorAccent">@android:color/white</item>-->
        <item name="android:textColorHint">#989898</item>
     <!--   <item name="colorControlNormal">@color/BackgroundtWhiteColor</item>
        <item name="colorControlActivated">@color/your color</item>
        <item name="colorControlHighlight">@color/BackgroundtWhiteColor</item>-->
    </style>

    <style name="Widget.App.Spinner" parent="@style/Widget.AppCompat.Spinner">
        <item name="overlapAnchor">true</item>
        <item name="android:background">@drawable/spinner_background</item>
    </style>

</resources>

2 个答案:

答案 0 :(得分:1)

您需要使用自定义样式覆盖文本输入布局的默认样式。

styles.xml

<resources>

 <style name="TextLabel">
    <item name="android:textColorHint">@color/colorPrimary</item> <!-- Your Hint Color -->
    <item name="android:textSize">18sp</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="colorControlNormal">@color/colorPrimary</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
</style>

<style name="error_appearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/colorAccent</item>
</style>

</resources>

activity_layout.xml

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:theme="@style/TextLabel"
            app:errorTextAppearance="@style/error_appearance">

            <EditText
                android:id="@+id/edt_mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_phone_iphone_black_24dp"
                android:drawablePadding="8dp"
                android:drawableTint="@color/colorPrimary"
                android:hint="@string/email"
                android:inputType="number"
                android:textColor="#000" />
        </android.support.design.widget.TextInputLayout>

重点:

Focused

没有重点:

Without focus

错误消息:

enter image description here

答案 1 :(得分:0)

您需要为其定义自己的样式,如下所示。在style.xml中。您可以定义任何样式。更改文本的颜色,大小和其他属性。

<style name="text_error_profile" parent="@android:style/TextAppearance">
    <item name="android:textColor">colorYouWant</item>
     <item name="android:textColorHint">colorHintyouwant</item>
</style>

在内部添加app:errorTextAppearance="@style/error_appearance",如下所示进行应用。

<android.support.design.widget.TextInputLayout
  .....
  ....
  app:errorTextAppearance="@style/text_error_profile"

您可以使用以下命令以编程方式更改提示颜色。验证失败后添加此内容。

 editText.setHintTextColor(hintcoloryouwant);