更改 Theme.MaterialComponents 的 TextInputLayout.OutlinedBox 的光标颜色

Ser*_*o76 5 android colors android-textinputlayout material-components-android

在我的应用程序中,我使用 textfield.TextInputLayout 和 textfield.TextInputEditText 作为输入文本,而不是通常的 EditText。这是我的观点:

 <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/inputId"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/fab_margin"
        android:layout_marginEnd="@dimen/fab_margin"
        android:focusedByDefault="false"
        android:enabled="true"
        android:hint="@string/userHint"
        android:textColor="@color/letter_medium_emphasis"
        app:boxCornerRadiusBottomEnd="@dimen/OutLinedBoxCorners"
        app:boxCornerRadiusBottomStart="@dimen/OutLinedBoxCorners"
        app:boxCornerRadiusTopEnd="@dimen/OutLinedBoxCorners"
        app:boxCornerRadiusTopStart="@dimen/OutLinedBoxCorners"
        app:boxStrokeColor="@color/letter_medium_emphasis"
        app:counterEnabled="true"
        app:counterMaxLength="20"
        app:helperText="@string/rememberId"
        app:helperTextEnabled="true"
        app:hintTextColor="@color/letter_medium_emphasis"
        app:startIconDrawable="@drawable/ic_account_circle_black_24dp"
        app:startIconTint="@color/primary_dark">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            android:inputType="text"
            android:fontFamily="@font/poppins_medium" />

    </com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

一切都是正确的,除了一件事,即光标和文本选择器的颜色。我附上一张图片来更好地解释自己。

在此输入图像描述

我一直在读,显然这已经发生在更多的人身上。光标在那里,但始终是白色的。

在此输入图像描述

我找到了解决方案,但它们都适用于 FilledBox.Dense 类型。我正在使用 OutlinedBox,但没有任何效果。我发现的最好的解决方案是这个

问题是,正如我所说,它适用于 FilledBox.Dense 类型,而不适用于 OutlinedBox。

有人可以告诉我如何适应这个解决方案或如何以其他方式改变颜色?

我尝试使用 OutlinedBox 类型对其进行调整,但没有成功。

提前致谢。

Sco*_*ggs 8

是的,这绝对是谷歌代码中的另一个错误,叹息。但这是我正在使用的解决方法。

定义您的风格(通常在 中styles.xml):

    <style name="Widget.MaterialComponents.TextInputLayout.OutlinedBox.MyStyle">
        <item name="shapeAppearance">@style/ShapeAppearance.MaterialComponents.Onboarding</item>
        <item name="boxStrokeColor">@color/onboarding_statelists</item>
        <item name="hintTextColor">@color/colorOnPrimary</item>
        <item name="colorControlActivated">@color/colorAccent</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

然后(这非常重要)在布局文件的小部件声明中,您必须引用该样式作为主题 样式!

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/name_et"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="type your name"

            android:theme="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.MyStyle"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.MyStyle"

            app:layout_constraintEnd_toEndOf="@id/end"
            app:layout_constraintStart_toStartOf="@id/start"
            app:layout_constraintTop_toBottomOf="@id/bottomLabel">
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

您确实不应该在这里将其定义为主题。在我的测试中,你必须两者都做!肯定有问题发生,但这对我有用。祝你好运!


Gre*_*ory 3

基于本文的更简单的解决方案: https://www.itcodar.com/android/set-edittext-cursor-color.html

本示例将光标颜色设置为colorAccent

1.将此样式添加到styles.xml文件中:

<style name="ThemeOverlay.AppTheme.TextInputEditText.Outlined" parent="">
    <item name="colorControlActivated">?attr/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

2.将这些styleandroid:theme属性添加到您的 TextInputLayout 视图中:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/my_field_id"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:theme="@style/ThemeOverlay.AppTheme.TextInputEditText.Outlined"
    ...
    >
    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)