TextInputLayout - 在EditText的非激活状态下更改浮动标签提示颜色

3

我正在使用带有浮动标签提示的TextInputLayout。但在正常状态下,我无法将提示颜色从白色更改为其他颜色。有没有办法做到这一点?

<android.support.design.widget.TextInputLayout
    android:id="@+id/fullNameTextLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_weight="0.75">
    <EditText
        android:id="@+id/etFullName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:singleLine="true"
        android:hint="Full Name"
        android:textColor="@color/gray_dark"
        android:textColorHint="@color/green"
        android:textColorHighlight="@color/green" />
</android.support.design.widget.TextInputLayout>

附上两张更换背景的屏幕截图。

绿色背景下的相同视图

白色背景下的相同视图


现在可能有点晚了,但是我成功地通过添加 android:textColorHint 到 TextInputLayout 中进行了操作。 - kristyna
5个回答

7
请在TextInputLayout中添加此内容。
  app:hintTextAppearance="@style/mytext 

所以,您的布局将会像这样:
 <android.support.design.widget.TextInputLayout
        android:id="@+id/aeal_input_layout_zipcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/green"
        app:hintTextAppearance="@style/mytext">
    <EditText
        android:id="@+id/aeal_etZipCode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Zipcode"
        android:singleLine="true"
        android:inputType="number"
        android:textColor="@color/primaryTextColor" />
</android.support.design.widget.TextInputLayout>

style.xml:

 <style name="mytext" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/green</item>
    <item name="android:textColorHint">@color/green</item>
    <item name="colorAccent">@color/green</item>
    <item name="android:textSize">14sp</item>
</style>

编辑:您需要在TextInputLayout中添加textColorHint,这样它就可以按照您的要求正常工作。

对我有用,也可能对您有帮助。


我尝试过这个方法。但是它们仍然显示为白色。我应该提到,我一开始就从代码中将所有编辑文本设置为不可聚焦。因此,在上面的图片中,它们并没有被激活。我希望它们即使处于非活动状态也能以绿色显示。这可能吗? - chaithu
我也尝试了将 focusable 设为 false,但是没有得到需要的结果。我尝试解决了相同的问题,并找到了解决方案,所以请检查我的编辑答案。 - user4571931

2

我曾经遇到过同样的问题,通过使用TextInputLayout的setHintTextAppearance(int)方法解决了这个问题。

例如:

首先找到你的TextInputLayout

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

现在请执行以下操作:

textInputLayout.setHintTextAppearance(R.style.Inactive);

在styles.xml文件中,您可以使用提示颜色来创建样式。例如:
<style name="Inactive" parent="AppThemeLight">
    <item name="android:textColorPrimary">@android:color/darker_gray</item>
    <item name="android:textColor">@android:color/darker_gray</item>
</style>

<style name="Active" parent="AppThemeLight">
    <item name="android:textColorPrimary">@android:color/black</item>
    <item name="android:textColor">@android:color/black</item>
</style>

现在,当你改变一些东西,例如onButtonClick时,你只需要改变你的样式。

final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
textInputLayout.setHintTextAppearance(R.style.Active);
             }
         });

请记住,您还需要处理TextInputLayout中EditText的颜色。

0

使用此功能更改提示颜色。-

editText.setHintTextColor(getResources().getColor(R.color.xyz));

你的问题的解决方案 -

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){
        //do something
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        //do something
    }

    @Override
    public void afterTextChanged(Editable arg0) {
        if(arg0.toString().length() <= 0) //check if length is equal to zero
            editText.setHintTextColor(getResources().getColor(R.color.xyz));
    }
});

0
在您的Style.xml文件中,您可以更改提示文字的颜色。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">#3498db</item>
</style>

更多信息请参见this


0

试试这个

<android.support.design.widget.TextInputLayout ........ android:textColorHint="requiredColor" ....> 来改变浮动提示颜色和

<EditText android:textColor="requiredColor" 来改变编辑框中的提示颜色


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接