【问题标题】:Androidx Material TextInput color doesn't changeAndroidx Material TextInput 颜色不会改变
【发布时间】:2019-03-05 11:45:08
【问题描述】:

我正在尝试在我的应用程序中创建一个com.google.android.material.textfield.TextInputLayout aka TextInputLayout 并根据字段中文本的存在来使提示文本更改其颜色。 现在这个元素的代码如下所示:

 <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/sign_up_email_text_input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/sign_up_email_hint"
            app:errorEnabled="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintVertical_bias="0.57"
            app:layout_constraintWidth_percent="0.78"
            android:textColorHighlight="#FF00FF"
            android:textColorHint="#00FF00"
            style="@style/TextInputLayout"
    >

        <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/sign_up_email_edit_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textEmailAddress"
        />
    </com.google.android.material.textfield.TextInputLayout>

我正在应用一种样式

<style name="TextInputLayout" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
    <item name="android:textColorHint">#FFFFFF</item>
    <item name="android:textColor">#FFFF00</item>
    <item name="colorAccent">#FF00FF</item>
    <item name="colorControlNormal">#FF0000</item>
    <item name="colorControlActivated">#00FF00</item>
    <item name="colorControlHighlight">#0000FF</item>
</style>

但是这种样式只适用于不活动的提示颜色,并且它对提示的两种状态应用相同的颜色 (android:textColorHint) - 当它填充 textField 和跳转到一个角落时。 当 TextInput 获得焦点时,标签将颜色更改为来自主题的colorAccent 颜色,而不是来自我应用的样式

我的问题是 - 如何仅使用 XML 使提示标签根据文本的存在(而不是 TextInput 是否聚焦)改变颜色?我以前(在支持库上)看到过人们这样做的例子,但我自己无法达到这样的结果。

【问题讨论】:

    标签: android material-design androidx


    【解决方案1】:

    迁移到 AndroidX 时遇到同样的问题。
    更改父主题

    <style name="TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
        <item name="android:textColorHint">#FFFFFF</item>
        <item name="android:textColor">#FFFF00</item>
        <item name="colorAccent">#FF00FF</item>
        <item name="colorControlNormal">#FF0000</item>
        <item name="colorControlActivated">#00FF00</item>
        <item name="colorControlHighlight">#0000FF</item>
    </style>
    

    【讨论】:

    • 尝试删除父级,因为它不是必需的。
    • @SarthakGandhi:是的,没有父母,它正在工作。谢谢
    • 尝试使用您的父主题 parent="Widget.MaterialComponents.TextInputLayout.FilledBox"。仍然无法正常工作,因为我希望它可以正常工作。当我完全删除父主题时没有任何变化
    【解决方案2】:

    遇到了同样的问题。问题在于 com.google.android.material:material:1.1.0-alphaXX。问题已通过恢复为 com.google.android.material:material:1.0.0 得到解决。

    旁注:在使用 com.google.android.material:material:1.1.0-alpha08 时,我什至无法对包含 TextInputLayout 的布局进行充气。 1.1.0 似乎充满了问题。

    【讨论】:

      猜你喜欢
      • 2014-12-17
      • 1970-01-01
      • 2016-09-11
      • 2021-08-01
      • 2021-03-16
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多