问题
As per Googles Material Guidelines:
https://material.io/guidelines/components/text-fields.html#text-fields-layout
TextInputLayout hint should be the same color as the Error message:
However it is not like that and when I call setError("My error")
only the underline and the error message show up in red.
How can I change this behavior to account for Google's own guidelines?
回答1:
here is how you can do it
<android.support.design.widget.TextInputLayout
android:padding="8dp"
android:id="@+id/tilSample"
app:errorTextAppearance="@style/error_appearance"
app:hintTextAppearance="@style/error_appearance"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etSample"
android:layout_margin="8dp"
android:padding="8dp"
android:hint="android"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
style.xml
<style name="error_appearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/colorRed</item>
<item name="android:textSize">12sp</item>
</style>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorRed">#f44336</color>
</resources>
EDIT
we can do manipulate the error and hint colours in code also using
tilSample.setErrorTextAppearance(R.style.error_appearance);
tilSample.setHintTextAppearance(R.style.error_appearance);
回答2:
Now this is default behaviour. To achieve this, just update your support library version to 28+.
implementation 'com.android.support:design:28.0.0'
回答3:
Here is an example:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email_layout"
android:textColorHint="@color/gray"
app:errorTextAppearance="@style/TextAppearence.App.TextInputLayout"
app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout">
<android.support.design.widget.TextInputEditText
android:id="@+id/et_email"
style="@style/TextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:cursorVisible="true"
android:gravity="center|left|bottom"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:maxLength="50"
android:paddingBottom="10dp"
android:textColor="@color/black_effective"
android:textSize="18sp" />
</android.support.design.widget.TextInputLayout>
Style File:
<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/gray</item>
</style>
Snapshot:
回答4:
TextInputLayout in Xml
<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:id="@+id/tx_login_password" app:errorEnabled="true" style="@style/text_input_style" app:passwordToggleEnabled="true" app:hintTextAppearance="@style/text_Apearence" android:layout_below="@id/tx_login_username" android:layout_centerHorizontal="true" app:theme="@style/Theme.App.Base" android:layout_marginLeft="70dp" android:layout_marginStart="70dp" android:layout_marginRight="70dp" android:layout_marginEnd="70dp" app:errorTextAppearance="@style/text_Apearence" android:layout_height="wrap_content"> <android.support.v7.widget.AppCompatEditText android:layout_width="match_parent" android:layout_marginTop="10dp" android:layout_height="wrap_content" android:hint="Password" android:paddingTop="5dp" android:typeface="sans" android:textStyle="bold" android:textSize="15sp" android:maxLines="1" android:inputType="textPassword" android:lines="1" android:elegantTextHeight="true" android:textAlignment="viewStart" android:layout_gravity="start" android:id="@+id/password" android:theme="@style/Theme.App.Base" app:theme="@style/Theme.App.Base" style="@style/EditText_style" /> </android.support.design.widget.TextInputLayout>
text_Apearence in style.xml file from resourses
<style name="text_Apearence" parent="TextAppearance.Design.Hint">
<item name="android:textSize">15sp</item>
<item name="textColor">@color/react_blue</item>
<item name="android:colorActivatedHighlight">@color/react_blue</item>
<item name="android:colorControlActivated">@color/react_blue</item>
</style>
1. 3. Style for Text Input Layout text_input_style XML file
<!-- begin snippet: js hide: false console: true babel: false -->
- Style for Text Input Layout text_input_style XML file
<style name="text_Apearence" parent="TextAppearance.Design.Hint">
<item name="android:textSize">15sp</item>
<item name="textColor">@color/react_blue</item>
<item name="android:colorActivatedHighlight">@color/react_blue</item>
<item name="android:colorControlActivated">@color/react_blue</item>
</style>
Image Looks Like
回答5:
<style name="text_Apearence" parent="TextAppearance.Design.Hint">
<item name="android:textSize">15sp</item>
<item name="textColor">@color/react_blue</item>
<item name="android:colorActivatedHighlight">@color/react_blue</item>
<item name="android:colorControlActivated">@color/react_blue</item>
</style>
来源:https://stackoverflow.com/questions/46668346/textinputlayout-hint-color-in-error-state