【问题标题】:Don't change textinputlayout hint color while setting error设置错误时不要更改 textinputlayout 提示颜色
【发布时间】:2020-01-27 11:02:16
【问题描述】:

我在 Android 中使用 textinputlayout 实现表单

我不想在 textinputlayout 上设置错误时更改提示文本颜色,如下所示

textInputLayout.setErrorEnabled(true)
textInputLayout.setError("this field is required ")

// 到目前为止,此代码正在将错误消息和提示颜色更改为红色。但我不想将提示颜色更改为红色。只有消息颜色应更改为红色

我想将错误提示“昵称”更改为蓝色,将错误消息更改为红色。

【问题讨论】:

标签: android android-layout


【解决方案1】:

TLDR;似乎没有办法或解决方法来做到这一点。

将提示颜色设置为与错误不同似乎无法通过 xml 样式实现。 This 和 this 不起作用。无论我使用什么组合,当设置错误(错误颜色)时,提示总是会改变。

所以我在TextInputLayout 代码中做了一些挖掘工作。它已经保存了提示颜色defaultHintTextColor,但状态变化时不使用此颜色:

private void updateLabelState(boolean animate, boolean force) {
...
 } else if (errorShouldBeShown) { 
 collapsingTextHelper.setCollapsedTextColor(indicatorViewController.getErrorViewTextColors());
...

collapsingTextHelper 似乎负责绘制提示(它是唯一使用提示颜色的)。但是,在发生错误后我设置的属性都没有工作hintTextColor、setHintTextAppearance、defaultHintTextColor(尝试使用postDelayed)。事实上,设置任何这些都可以触发collapsingTextHelper.setCollapsedTextColor(hintTextColor);,但总会有更新触发上述代码:

      if (defaultHintTextColor == null) {
        collapsingTextHelper.setCollapsedTextColor(hintTextColor);
      }

      focusedTextColor = hintTextColor;

      if (editText != null) {
        updateLabelState(false);
      }

如果您调试或检查布局检查器,您将看不到提示的相应文本视图。文字是由collapsingTextHelper绘制的,所以遗憾的是没有办法获得提示视图。

【讨论】:

    【解决方案2】:

    设置为应用程序的主题,它仅适用于仅突出显示状态

     <item name="colorAccent">@color/Color Name</item>
    

    【讨论】:

      【解决方案3】:

      试试下面的代码,它在正常状态下工作

      如下所示更改 TextInputLayout 的主题

      <com.google.android.material.textfield.TextInputLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/ti_email"
              android:hint="@string/EmailAddress"
              android:layout_toLeftOf="@+id/imEntremail"
              android:textColorHint="@color/view"
              app:hintTextAppearance="@style/HintTextStyle">
      
              <EditText
                  android:id="@+id/etentrEmail"
                  android:layout_width="match_parent"
                  android:layout_height="@dimen/_30sdp"
                  android:layout_marginTop="@dimen/_10sdp"
      android:cursorVisible="false"
                  android:imeOptions="actionNext"
      android:padding="@dimen/_3sdp"
                  android:gravity="left|bottom"
                  android:textColorHint="#B8B8B8"
                  android:textSize="@dimen/_11ssp"
                  android:maxLines="1"
                  android:inputType="text"
                  android:background="@android:color/transparent"
                  android:textColor="#000000"/>
          </com.google.android.material.textfield.TextInputLayout>
      

      在 Styles 文件夹 HintTextStyle 代码中更改 android:textColor 代码以更改浮动标签颜色

      <style name="HintTextStyle" parent="TextAppearance.Design.Hint">
          <item name="android:textSize">14sp</item>
          <item name="android:padding">26dp</item>
          <item name="android:textColor">#ff0000</item>
      </style>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-05
        • 2015-08-29
        • 2015-08-15
        • 2021-06-21
        • 2018-03-21
        • 2018-11-03
        • 2015-09-15
        相关资源
        最近更新 更多