【问题标题】:Underline, cursor and hint disappeared at TextInputEditText in TextInputLayout on focus下划线、光标和提示在 TextInputLayout 中的 TextInputEditText 处消失
【发布时间】:2019-10-01 09:57:52
【问题描述】:

我有一个带有一堆 TextInputEditTexts 的编辑配置文件屏幕。以前效果很好,但现在焦点下划线,光标和提示变得不可见。

有人遇到过同样的问题吗?

...

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilFirstName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/ivContactIcon"
        app:layout_constraintEnd_toEndOf="@id/gEnd"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        android:layout_marginStart="@dimen/margin_32"
        android:layout_marginTop="@dimen/margin_24"
        android:hint="@string/profile_edit_hint_first_name"
        >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etFirstName"
            android:layout_height="wrap_content"
            style="@style/FontRoboRegularSizeMFontPrimaryOneLineMatchWrap"
            tools:text="Oleh"
            android:inputType="textCapWords"
            android:maxLines="1"
            android:nextFocusForward="@id/etLastName"
            android:imeOptions="actionNext"
            />
    </com.google.android.material.textfield.TextInputLayout>

...

更新: 更改根元素的背景后,很明显这些元素变为白色。没有消失。

【问题讨论】:

  • 什么是 style="@style/FontRoboRegularSizeMFontPrimaryOneLineMatchWrap" ?
  • @dimen/text_size_m@color/new_font_primary_color@string/font_roboto_regularmatch_parent
  • @GabrieleMariotti 我刚刚改变了背景,发现提示、光标和下划线都变成了白色。
  • 很奇怪。与屏幕中的其他 textinputlayout 有什么区别?
  • 你是如何改变背景的?我没有看到它的组件

标签: android android-appcompat android-textinputlayout material-components-android android-textinputedittext


【解决方案1】:

TextInputLayout使用的默认样式是

<style name="Widget.MaterialComponents.TextInputLayout.FilledBox" parent="Base.Widget.MaterialComponents.TextInputLayout">
    <!-- underline color in FilledBox style -->
    <item name="boxStrokeColor">@color/mtrl_filled_stroke_color</item>

    <!-- The color of the label when it is collapsed and the text field is active -->
    <item name="hintTextColor">?attr/colorPrimary</item>
    ....
</style>

mtrl_filled_stroke_color 基于colorOnSurface

在您的主题中检查colorPrimarycolorOnSurface 值,或使用具有上述相同属性的自定义样式。

【讨论】:

  • 这需要从 AppCompat Theme 切换到 Theme.MaterialComponents。有什么方法可以在不切换的情况下修复外观?
  • 你的评论很奇怪。您正在使用材料组件库提供的组件com.google.android.material.textfield.TextInputEditText。它需要Material Components theme
  • 我的主题是 Theme.AppCompat.Light.NoActionBar 的继承者
  • 您可以查看文档。使用版本 1.1.0 it will not be possible。在任何情况下,组件都使用答案中描述的样式。您可以尝试强制使用另一种样式,但效果会很差。
  • @OlehLiskovych 这也取决于您使用的版本。在任何情况下,如果您的应用中没有 Material Theme,您将无法使用此样式。
【解决方案2】:

如果您像我一样苦苦挣扎,并且上述解决方案不起作用,则可能是您的项目主题设置为白色为主。

要解决此问题,您可以为 TextEdits 创建自定义样式并将其应用于每个:

styles.xml:

<style name="AppThemeCorrectPrimaryColor" parent="AppTheme">
    <item name="colorPrimary">@color/notAWhiteColor</item>
</style>

编辑文本:

<com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:theme="@style/AppThemeCorrectPrimaryColor"
                    />

【讨论】:

    【解决方案3】:

    我为TextInputLayout 创建了样式并定义了colorControlActivated 之类的

    <style name="Widget.AppTheme.TextInputLayout.FilledBox.Dense" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
            <item name="hintTextColor">@color/black</item>
            <item name="boxBackgroundColor">@color/greyLight</item>
            <item name="boxStrokeWidth">0dp</item>
            <item name="colorControlActivated">@color/orange</item>
        </style>
    

    TextInputLayout上使用主题而不是样式

    <com.google.android.material.textfield.TextInputLayout
            android:theme="@style/Widget.AppTheme.TextInputLayout.FilledBox.Dense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </com.google.android.material.textfield.TextInputLayout>
    

    【讨论】:

      猜你喜欢
      • 2017-04-18
      • 2020-01-20
      • 1970-01-01
      • 2021-08-25
      • 2018-03-15
      • 2019-06-10
      • 1970-01-01
      • 2019-07-15
      • 2019-11-25
      相关资源
      最近更新 更多