Android 输入布局切换hintText颜色更改

Android 输入布局切换hintText颜色更改,android,android-layout,Android,Android Layout,我使用的是TextInputLayout,我想更改hinttext的颜色。当我输入文本时,提示文本的颜色应为蓝色,其他颜色应为灰色,当edittext包含一些值时,则hinttext的颜色应为蓝色。有关详细信息,请查看我的图像 最终我想在编辑包含一些值的文本后使用它 这是我使用此代码后的当前结果 <style name="labelcolor" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Hint color

我使用的是
TextInputLayout
,我想更改
hinttext
的颜色。当我输入文本时,提示文本的颜色应为蓝色,其他颜色应为灰色,当
edittext
包含一些值时,则
hinttext
的颜色应为蓝色。有关详细信息,请查看我的图像

最终我想在编辑包含一些值的文本后使用它

这是我使用此代码后的当前结果

  <style name="labelcolor" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">#868686</item>
    <item name="android:textSize">16sp</item>
    <item name="android:paddingTop">5sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">#0ea3ff</item>
    <item name="colorControlNormal">#0ea3ffr</item>
    <item name="colorControlActivated">#0ea3ff</item>
</style>


#868686
16便士
5便士
#0ea3ff
#0ea3ffr
#0ea3ff

请帮助我如何解决此问题。

创建您的as-bellow并重试

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

@android:彩色/白色
@颜色/背景白色
@颜色/背景白色
@颜色/你的颜色
@颜色/背景白色
用它作为吼叫

<android.support.design.widget.TextInputLayout
            android:theme="@style/EditTextHint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
      <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>

首先,您需要设置TextInputLayout

 android.support.design.widget.TextInputLayout textInputLayout =        (TextInputLayout) view.findViewById(R.id.textInputLayout);

textInputLayout.setHintTextAppearance(R.style.Instyle);
然后在styles.xml文件中,您可以使用提示颜色的颜色创建样式

<style name="Instyle" parent="AppThemeLight">
    <itemname="android:textColorPrimary">@android:color/darker_gray</item>
    <item name="android:textColor">@android:color/darker_gray</item>
</style>

@android:颜色/深灰色
@android:颜色/深灰色

TextInputLayout在您开始在edittext中输入值时将
colorAccent
转换为hintTextColor。 因此,如果您希望它是蓝色的,那么在您的主题中将
colorAccent
设置为蓝色。当edittext没有值时,将hintTextColor设置为灰色。你喜欢这样吗

<android.support.design.widget.TextInputLayout
                    android:id="@+id/userNameTIL"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@color/spinnerBg"
                    android:clipToPadding="false"
                    android:gravity="bottom"
                    android:paddingTop="4dp"
                    android:textColorHint="@color/grey"> /*Here it sets color to hintText when edittext has no value */

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:layout_gravity="bottom"
                        android:background="@null"
                        android:hint="Username"
                        android:inputType="text"
                        android:maxLength="30"
                        android:paddingEnd="3dp"
                        android:paddingStart="25dp"
                        android:paddingTop="3dp"
                        android:singleLine="true"
                        android:textColor="@color/black"
                        android:textColorHint="@color/black"
                        android:textSize="20sp"/>
                </android.support.design.widget.TextInputLayout>
/*在这里,当edittext没有值时,它将颜色设置为hintText*/

@颜色/默认值\u应用程序\u白色
@颜色/默认值\应用程序\绿色
@颜色/默认值\应用程序\绿色
@颜色/默认值\u应用程序\u白色

试试这个,但它会影响整个应用程序

更改TextInputLayout Lebel颜色动态更改edittext的焦点并根据我的工作传递颜色

 public static void textInoutLayoutColor(TextInputLayout textInputLayout, @ColorInt int color) {

    try {
        Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
        fDefaultTextColor.setAccessible(true);
        fDefaultTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));

        Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
        fFocusedTextColor.setAccessible(true);
        fFocusedTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
然后像这样改变颜色

input_layout= (TextInputLayout) findViewById(R.id.input_layout);
   etuser_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasfocus) {
            if(hasfocus){
                textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));
            }
            else
            {
                if(etuser_name.getText().toString().trim().length()>0) {
                    textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));;
                }
                else
                {
                    textInoutLayoutColor(input_layout,getResources().getColor(R.color.hint_grey));
                }
            }
        }
    });

一旦开始键入提示文本,提示文本将不可见,因此无法更改提示文本的颜色。您可以更改您输入的文本的颜色。我必须更改电子邮件的标签颜色。检查此答案我只使用了此项。
input_layout= (TextInputLayout) findViewById(R.id.input_layout);
   etuser_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasfocus) {
            if(hasfocus){
                textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));
            }
            else
            {
                if(etuser_name.getText().toString().trim().length()>0) {
                    textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));;
                }
                else
                {
                    textInoutLayoutColor(input_layout,getResources().getColor(R.color.hint_grey));
                }
            }
        }
    });