TextInputLayout 提示文字颜色。

14

我正在使用新的Google设计库(com.android.support:design:22.2.0),但是我在使用android.support.design.widget.TextInputLayout时遇到了问题。

如果我通过编程方式设置EditText,则浮动提示颜色是灰色而不是强调颜色。如果用户自己填写字段(EditText)或更改字段的预定义值,则它会起作用。

这是我的代码:

<android.support.design.widget.TextInputLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <EditText
            android:id="@+id/register_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/register_username"
            android:inputType="text"/>
</android.support.design.widget.TextInputLayout>

用户点击某个按钮后,它通过EditText的setText()方法填充字段,浮动提示变为灰色。这是一个 bug 吗,还是我漏掉了什么?


请查看我在这里的答案,以更改您选择的标签颜色:链接 - Summved Jain
4个回答

24
android:textColorHint="@color/your_color" 放置在 TextInputLayout 内部。

6
请使用android.support.v7.widget.AppCompatEditText代替EditText,这样您的问题应该可以得到解决。同时,请确保您的gradle设置如下所示:
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:design:22.2.0'

1
最新的支持库已经不需要这样做了。 - IgorGanapolsky
正确,这个问题是在我们第一次获得TextInputLayout时出现的。 - Lucas Crawford

4

请查看此处

特别是android.support.design:hintTextAppearance="@style/TextAppearance.AppCompat">


提示颜色没问题,只有当我通过编程方式设置EditText的文本时才会出现问题。我认为它应该自动使用强调颜色。我尝试了这个解决方法,但我不喜欢这个解决方案 :-) - Veronnie
我尝试了但没有成功。即使调用setText方法后,提示仍然变灰。 - Veronnie

1
以下代码适用于我:

<android.support.design.widget.TextInputLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="16dp">

         <EditText
              android:id="@+id/email"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/white_box"
              android:hint="@string/prompt_email"
              android:inputType="textEmailAddress"
              android:maxLines="1"
              android:singleLine="true"
              android:textColor="@color/text_color"
              android:textColorHighlight="@color/text_color"
              android:textColorHint="@color/text_color"
              android:drawableRight="@drawable/ic_action_email"/>
 </android.support.design.widget.TextInputLayout>

textColorHint是编辑文本框未获得焦点时的提示颜色,而android:textColorHighlight则是浮动模式下的提示颜色。


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