【问题标题】:Use custom hint text color for hints in enabled and disabled views为启用和禁用视图中的提示使用自定义提示文本颜色
【发布时间】:2019-07-08 16:09:05
【问题描述】:

我正在尝试将自定义样式添加到 TextInputLayout 和 TextInputEditText,但我无法获得预期的结果。 我需要的是自定义颜色“A”用于启用视图中的提示,自定义颜色“B”用于禁用视图中的提示。

现在我有一个样式,带有启用/禁用提示的选择器。风格是这样的:

<style name="CustomTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="boxStrokeColor">@color/input_blue</item>
    <item name="boxStrokeWidth">2dp</item>
    <item name="errorTextAppearance">@style/ErrorText</item>
    <item name="android:textColorHint">@color/selector_edithintcolor</item>
</style>

这里是选择器,可以看颜色来说明问题:

现在布局中的一些组件:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/layout_text_disabled"
    style="@style/CustomTextInputLayoutStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/layout_phone_not_focused">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/txt_text_disabled"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_deactivated"
        android:inputType="text"
        android:text="@string/txt_disabled" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/layout_text_disabled_hint"
    style="@style/CustomTextInputLayoutStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/layout_text_disabled">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/txt_text_disabled_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_deactivated"
        android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>

此外,我还有一个按钮可以将这两个视图的状态从启用更改为禁用,这是行为:

启用视图后,一切都按预期进行,提示根据选择器显示为绿色:

现在,当我禁用视图时,我得到的是:

根据选择器使禁用提示变为橙色的任何想法?提前致谢!

【问题讨论】:

标签: android material-design android-styles android-textinputlayout


【解决方案1】:

按照link 看来,现在可以使用 com.google.android.material:material 的 1.2.0-alpha03 版本 您需要像这样创建一个颜色状态列表:

box_stroke_color_state_list.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?colorPrimary" android:state_enabled="true" />
    <item android:color="?colorSecondary" android:state_hovered="true" />
    <item android:color="?colorSecondary" android:state_focused="true" />
    <!-- This is where the disable hint and box will take it's color -->
    <item android:alpha="@dimen/material_emphasis_disabled" android:color="@android:color/holo_green_dark" android:state_enabled="false" />
    <item android:color="@color/mtrl_textinput_default_box_stroke_color" />
</selector>

然后在你的代码中调用它:

ContextCompat.getColorStateList(context, R.color.box_stroke_color_state_list)?.let {
    layout_text_disabled_hint.setBoxStrokeColorStateList(it)
}

【讨论】:

    【解决方案2】:

    根据@Caktuspace的回复,可以这样申请:

    layout.xml

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til"
        ...
        android:hint="@string/hello"
        android:textColorHint="@color/hint_text_color_textinputlayout">
    

    hint_text_color_textinputlayout.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:alpha="@dimen/material_emphasis_disabled" android:color="@color/desiredColorDisabled" android:state_enabled="false" />
        <item android:color="@color/desiredColorEnabled" />
    </selector>
    

    【讨论】:

      猜你喜欢
      • 2015-06-29
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多