更改TextInputEditText提示颜色

时间:2017-07-12 16:22:02

标签: android android-layout android-textinputlayout

我想更改TextInputEditText提示的文本颜色。似乎无论我改变什么,颜色总是主应用主题的颜色。

<style name="EditTextBaseStyle" parent="Widget.AppCompat.EditText">
    <item name="android:textViewStyle">@style/TextViewStyle</item>
    <item name="android:textColor">@color/middle_black</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textSize">16sp</item>
</style>

基础风格

android:textColorHint="@color/warm_grey"

我尝试过添加

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/primary</item>
    <item name="android:textColorHint">@color/primary_text</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowSoftInputMode">stateHidden|adjustPan</item>
</style>

对基本风格以及我的AppTheme而言并没有帮助。

这是我的AppTheme

from graph import adj_mtx, AP
import heapq as hq

lv, visited, h = float('inf'), {}, []  # lv stands for 'large_value', h is the heap


def prims_mst(adj_matrix, src):
    hq.heappush(h, (0, (src, None)))  # O(logn)
    curr_dist = {item.value: lv if item.value != src else 0 for item in AP}  # AP is the enumeration of nodes

    while len(h) != 0:
        curr_nd = hq.heappop(h)[1][0]  # first element of the tuple is the value, second is the node  # O(1)
        visited[curr_nd] = True  # O(1)
        for nd, dst in enumerate(adj_matrix[src]):  # O(n) -> n is the number of nodes
            if nd not in visited and curr_dist[nd] > curr_dist[curr_nd] + adj_matrix[curr_nd][nd]:
                curr_dist[nd] = curr_dist[curr_nd] + adj_matrix[curr_nd][nd]
                hq.heappush(h, (curr_dist[nd], (nd, curr_nd)))  # O(logn)
        print h

我在这里缺少什么?

谢谢, Otterman

编辑:当字段具有焦点时,提示是正确的颜色。当字段获得焦点时,提示移动到字段上方并显示不正确的颜色。

5 个答案:

答案 0 :(得分:7)

对我来说,解决方法是在TextInputLayout上设置提示和textColor,而不是在TextInputEditText上进行设置,如下所示: https://material.io/develop/android/components/text-input-layout/

    <android.support.design.widget.TextInputLayout
        android:id="@+id/passwordTextInputLayout"
        android:hint="@string/add_nitrogen_template_name_label"
        android:textColorHint="@color/warm_grey">

        <android.support.design.widget.TextInputEditText
            android:textAppearance="@style/TextAppearance.Regular"
            android:textColor="@color/white"/>
    </android.support.design.widget.TextInputLayout>

答案 1 :(得分:3)

您是否尝试了AppTheme的这些属性?

<item name="colorControlNormal">@color/primary</item>
<item name="colorControlHighlight">@color/warm_grey</item>      
<item name="colorControlActivated">@color/warm_grey</item>

或移动

android:textColorHint="@color/warm_grey"

将XML小部件格式化为EditTextBaseStyle

答案 2 :(得分:2)

试试这个:

 <style name="AppTheme.EditTextBaseStyle" parent="@android:style/TextAppearance">
    <item name="android:textColor">@android:color/darker_gray</item>
</style>

答案 3 :(得分:1)

我能够通过添加

成功地使其成功运行
app:hintTextAppearance="@style/EditTextHintStyle"

到我的TextInputLayout

这是正确的风格

<style name="EditTextHintStyle" parent="EditTextBaseStyle">
    <item name="android:textColor">@color/warm_grey</item>
</style>

答案 4 :(得分:1)

您可以在布局中使用 app:hintTextColor android:textColorHint 属性:

<com.google.android.material.textfield.TextInputLayout
    app:hintTextColor="@color/mycolor"
    android:textColorHint="@color/text_input_hint_selector"
    .../>

或者您可以以自定义样式定义它们:

<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <!-- The color of the label when it is collapsed and the text field is active -->
    <item name="hintTextColor">@color/my_color</item>

    <!-- The color of the label in all other text field states (such as resting and disabled) -->
    <item name="android:textColorHint">@color/my_selector_color</item>
</style>