【问题标题】:TextInputLayout - Change floating label hint color if EditText is not empty and not focusedTextInputLayout - 如果 EditText 不为空且未聚焦,则更改浮动标签提示颜色
【发布时间】:2017-10-16 15:11:33
【问题描述】:

Res state - 提示颜色黑色,好

聚焦状态,空 - 提示颜色灰色,好

聚焦状态,不为空-提示颜色灰色,好

res状态,不为空-提示颜色黑色,不好,应该是灰色

如果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>

【问题讨论】:

  • @quicklearner 抱歉,我没有找到适合我的正确答案。
  • 你好@RexHunt!我面临着和你一样的问题。我想知道您是否找到了解决此问题的方法。
  • 你好@Marlon!不幸的是没有。
  • 您好@RexHunt,如果您找到了解决此问题的方法,请分享。

标签: android android-textinputlayout


【解决方案1】:

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

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

如果您想清除编辑文本的焦点,请使用

editText.clearFocus();

【讨论】:

  • 我认为您的建议是针对 editText 提示,但我需要浮动提示,如果可能,不要以编程方式
【解决方案2】:

改变你的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>

【讨论】:

  • 对不起,这对我没有帮助
  • 我已经测试过,然后发布了这个答案,你遇到了什么问题?
  • 在您的示例中,提示颜色始终相同,但仅当 editText 为空且未聚焦时才需要提示颜色黑色,否则为灰色。
  • 改变这一行的颜色 @color/colorBlack
  • 并且在这一行也改变了 @color/colorGray
【解决方案3】:

试试下面的代码:

<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>

【讨论】:

  • 提示颜色始终为黑色
  • 对不起,我没听明白,你要什么?
  • 只有在editText为空且未聚焦时才需要提示颜色黑色,否则为灰色。
  • "android:textColorHint" 代表提示的禁用颜色,这是我需要的。谢谢!
猜你喜欢
  • 2016-11-14
  • 2020-03-20
  • 2016-06-14
  • 2020-01-25
  • 2020-06-05
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
相关资源
最近更新 更多