【问题标题】:TextInputLayout change hint color when floatingTextInputLayout 浮动时更改提示颜色
【发布时间】:2020-06-05 22:35:26
【问题描述】:

就像那里的数百个问题一样,我想更改提示的颜色。最初我想在 cmets 中询问另一个此类问题(未回答),但我没有足够的声誉,所以我在这里问。

我希望 TextInputEditText 中的提示是一种颜色,而当它浮动时是另一种颜色。 在其他帖子中,答案始终是当视图聚焦时如何更改提示。 这不是我想要的,我可以做到。如果编辑文本不为空,我想更改提示颜色。我正在使用材料设计 textInputLayout 和 textInputEditText。尝试了不同的样式和选择器,但仍然无法做到。试图扩展 TextInputLayout 但我不知道如何将其绑定到 EditText 以便我可以将 OnTextListener 用于子视图。

到目前为止,我的代码只能在视图聚焦时更改提示的颜色。

样式:

<style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
    <item name="colorControlNormal">@color/green_03</item>
    <item name="colorControlActivated">@color/green_03</item>
    <item name="hintTextColor">@color/dark_blue_60</item>
</style>

<style name="MyHintStyle" parent="@android:style/TextAppearance">
    <!-- Hint color when floating and focused-->
    <item name="android:textColor">@color/dark_blue_60</item>
    <!-- doesn't change anything -->
    <item name="android:textColorHint">@color/dark_blue</item>
    <item name="colorAccent">@color/green_03</item>

    <item name="colorControlNormal">@color/green_03</item>
    <item name="colorControlActivated">@color/green_03</item>

</style>

XML:

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:textColorHint="@color/dark_blue"
            android:theme="@style/TextInputLayoutAppearance"
            app:errorEnabled="true"
            app:hintTextAppearance="@style/MyHintStyle">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/textInputEditText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:backgroundTint="@color/green_03"
                android:hint="@string/phoneNumber"
                android:textColor="@color/dark_blue"
                android:inputType="number"
                android:maxLength="3"
                android:text="12" />

        </com.google.android.material.textfield.TextInputLayout>

有没有办法为所有视图以编程方式扩展和更改它,或者设置一个全局监听器或使用样式和选择器的正确方法。

【问题讨论】:

  • 设置 TextInputLayout Theme ="MyHintStyle" 并删除 app:hintTextAppearance="@style/MyHintStyle" 后尝试。
  • 还是一样。如果焦点颜色为深蓝色,则默认为灰色
  • 您必须使用hintTextColorandroid:textColorHint 属性。检查这个answer
  • 只有在聚焦时才会改变提示

标签: android android-textinputlayout android-textinputedittext


【解决方案1】:

我认为您可以检查 editext 是否包含某些内容或是否是焦点。如果是这样 - 那么你改变提示的颜色。 检查这个的最佳位置 - EditText 的 View.FocusListener。

【讨论】:

  • 试过了。我扩展了编辑文本并添加了焦点侦听器,但您无法从编辑文本更改浮动提示的颜色。所以我想获得编辑文本的父视图,但没有成功。如果你知道怎么做,请告诉我
  • setHintTextAppearance 在 TextInputLayout 是你需要的。您可以传递样式资源以设置提示样式
  • setHelperTextColor 不用于提示,我将什么风格传递给hintTextAppearance。我尝试通过 state_active、state_focused、state_normal、state_enabled - 没用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 2021-06-21
  • 1970-01-01
  • 2016-06-14
  • 2015-08-15
  • 1970-01-01
相关资源
最近更新 更多