How to change the floating hint color of TextInput

2019-01-26 21:16发布

问题:

I am using EditText with TextInputLayout. I just want to change the floating hint color of TextInputLayout if the EditText is disabled. I tried with selector it is not working.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:textColor="@color/darkGray" />
    <item android:state_pressed="false" android:state_focused="false" android:textColor="@color/lightGray"/>
</selector>

回答1:

There's one method called setHintTextAppearance(int styleId) in TextInputLayout class. You can use this method to set difference colors to hint text on basis of enabled/disabled state.

Example :

//for disabled editText
mEditText.setEnabled(false);
mTextInoutLayout.setHintTextAppearance(R.styles.CustomHintDisabled);

//for enablededitText
mEditText.setEnabled(true);
mTextInoutLayout.setHintTextAppearance(R.styles.CustomHintEnabled);

And in your styles.xml

<style name="CustomHintDisabled" parent="YourBaseTheme.TextAppearance">
    <item name="textColor">@color/gray</item>
</style>

<style name="CustomHintEnabled" parent="YourBaseTheme.TextAppearance">
    <item name="textColor">@color/black</item>
</style>


回答2:

If you want to change hint of every TextInputLayout with same color you can change it from style with below code.

  <style name="income" parent="TextAppearance.AppCompat">
        <item name="android:textColor">@color/green</item>
        <item name="android:textColorHint">@color/green</item>
        <item name="colorAccent">@color/green</item>
        <item name="colorControlNormal">@color/green</item>
        <item name="colorControlActivated">@color/green</item>
        <item name="colorControlHighlight">@color/green</item>
        <item name="android:textColorHighlight">@color/green</item>
    </style>

here I have used this code for only one TextInputLayout and it has changed its hint color, text color, color of textinputlayout when selected, unselected too.

 <android.support.design.widget.TextInputLayout
                android:id="@+id/input_income"
                style="@style/income"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/input_days"
                android:layout_margin="10dp"
                android:hint="Enter Monthly Take Home Income"
                android:textColorHint="#18c418"
                android:theme="@style/income"
                app:hintAnimationEnabled="true">

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

Using android:theme is important to apply it for lollipop and above.



回答3:

Try app:hintTextAppearance in your android.support.design.widget.TextInputLayout. By defining a style for app:hintTextAppearance, you can also simply set the color of floating label. in styles.xml:

<style name="CustomTextAppearance" parent="@android:style/TextAppearance">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/colorAccent</item>

and in your TextInputLayout:

<android.support.design.widget.TextInputLayout
android:id="@+id/lyt_goal"
style="@style/CustomTextInput"
app:hintTextAppearance="@style/CustomTextAppearance">

<android.support.v7.widget.AppCompatEditText
    android:id="@+id/edt_goal"
    style="@style/CustomEditText"
    android:hint="@string/str_goal" />

Hope this will help you.



回答4:

Do following steps :- 1. Add this style into your styles.xml

<style name="FloatingLabel" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/theme</item>
    <item name="android:textSize">12sp</item>
</style>

2.Use it like bellow

<android.support.design.widget.TextInputLayout
                    android:id="@+id/loginPassLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerHorizontal="true"
                    android:layout_toRightOf="@+id/user"
                    android:textColorHint="@color/white"
                    foo:hintTextAppearance="@style/FloatingLabel">

                    <com.app.Widget.EditTextPlus
                        android:id="@+id/edt_email"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="5dp"
                        android:background="@android:color/transparent"
                        android:hint="Email"
                        android:inputType="textEmailAddress"
                        android:maxLength="30"
                        android:maxLines="1"
                        android:paddingLeft="10dp"
                        android:text=""
                        android:textColor="@color/white"
                        android:textColorHint="@color/white"
                        android:textSize="15sp"/>
                </android.support.design.widget.TextInputLayout>


回答5:

You can set the theme in your TextInputLayout

<android.support.design.widget.TextInputLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:theme="@style/TextLabel">

     <EditText
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:hint="First Name"
         />

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

<style name="TextLabel" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/colorHint</item> 
    <item name="android:textSize">16sp</item>

    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlNormal">@color/colorNormal</item>
    <item name="colorControlActivated">@color/colorActivated</item>
 </style>

For other EditTextlayout it will pick color from colorAccent defined in your application theme.

<item name="colorAccent">@color/colorAccent</item>

Hope it will help you.



回答6:

You have to define android:textColorHint in your application theme:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>

    <item name="android:textColorHint">@color/secondary_text</item>
</style>

Hope this works for you.