TextInputLayout - 如果EditText不为空且没有焦点,如何更改浮动标签提示颜色

7

Res状态-提示颜色黑色,良好
enter image description here

聚焦状态、为空-提示颜色灰色,良好
enter image description here

聚焦状态、非空-提示颜色灰色,良好
enter image description here

Res状态、非空-提示颜色黑色,不好,请改为灰色
enter image description here

如何在editText非空且未聚焦时更改提示颜色?

这是我的当前代码:

<android.support.design.widget.TextInputLayout
    android:id="@+id/email"
    style="@style/AuthInput"
    android:layout_marginTop="32dp"
    android:hint="@string/hint_email"
    android:inputType="textEmailAddress"
    android:theme="@style/AuthInput"
    app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout">

    <EditText
        android:id="@+id/edit_email"
        style="@style/AuthEditText"
        android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>

以及样式:

<style name="AuthInput">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColorHint">@color/black</item>
    <item name="android:paddingLeft">0dp</item>
</style>

<style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/inputHintActive</item>
</style>

https://dev59.com/wl0Z5IYBdhLWcg3w_0h8 - Quick learner
@quicklearner 抱歉,但我没有找到适合我的正确答案。 - RexHunt
你好@RexHunt!我和你一样也遇到了同样的问题。想知道你是否已经找到了解决办法? - Marlon
你好,@Marlon!很抱歉,不行。 - RexHunt
嘿,你找到这个问题的解决方案了吗? - Vaishali Sharma
显示剩余2条评论
3个回答

1
请尝试以下代码:
<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/black</item>
    <item name="colorControlNormal">@color/transparant</item>
    <item name="colorControlHighlight">@color/black</item>
    <item name="colorControlActivated">@color/transparant</item>
    <item name="android:textColorHint">@color/black</item> // set grey color for hint
</style>

Screenshot


提示颜色始终为黑色。 - RexHunt
不好意思,我没听懂你的问题。 - Bhavnik
只有当 editText 为空且未获得焦点时,我需要提示颜色为黑色,否则为灰色。 - RexHunt
"android:textColorHint" 代表禁用状态下的提示文本颜色,这正是我所需要的。谢谢! - Kirill Starostin

0

您可以通过以下方式更改提示颜色:

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

如果你想清除编辑文本的焦点,那么可以使用以下代码:

editText.clearFocus();

我认为你的建议是针对editText提示文本,但我需要浮动提示文本,如果可能的话,不需要编程实现。 - RexHunt

0

更改您的TextInputLayout样式

您的TextInputLayout

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

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/edit_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="hintText"/>

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

您的TextInputLayout样式会根据您的文本颜色而改变

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

    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="colorControlNormal">@color/colorPrimaryDark</item>
    <item name="colorControlActivated">@color/colorGray</item>
</style>

抱歉,但这并没有帮助我。 - RexHunt
我已经测试过并发布了这个答案,你还遇到什么问题了吗? - MilapTank
在你的例子中,提示颜色始终相同,但当编辑框为空且未聚焦时,我需要提示颜色为黑色,否则为灰色。 - RexHunt
更改此行中的颜色 <item name="android:textColorHint">@color/colorBlack</item> - MilapTank
我已经检查过了,当我们留下文本输入时,提示颜色仍然是黑色的,但我需要它变成灰色。请再次查看图片和相关注释。对于最后一张图片,我说“Res状态不为空-提示颜色为黑色,不好,应该是灰色”。如果我们在样式中更改提示颜色,它将会影响到其他状态。 - RexHunt
显示剩余5条评论

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