无法更改Material TextInputEditText的光标和气泡颜色

时间:2019-02-01 21:27:30

标签: java android xml material-design

我想改变材料TextInputEditText的气泡和光标的颜色。我尝试了colorAccent,android:textCursorDrawable,它们无法正常工作。

Files in here

You can see it in this image

3 个答案:

答案 0 :(得分:3)

只需在应用主题(白天/黑夜)中应用 colorControlActivated,所有 Material TextInputEditField 都会改变光标颜色

<item name="android:colorControlActivated">@color/green</item>

答案 1 :(得分:1)

您必须使用以下属性:

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <item name="colorPrimary">#212121</item>
    <item name="colorPrimaryVariant">#000000</item>
    <item name="colorOnPrimary">#FFFFFF</item>
    <item name="colorSecondary">#2962FF</item>
    <item name="colorSecondaryVariant">#0039CB</item>
    <item name="colorOnSecondary">#FFFFFF</item>
    <item name="colorError">#F44336</item>
    <item name="colorOnError">#FFFFFF</item>
    <item name="colorSurface">#FFFFFF</item>
    <item name="colorOnSurface">#212121</item>
    <item name="android:colorBackground">@color/background</item>
    <item name="colorOnBackground">#212121</item>
</style>

<color name="background">#FAFAFA</color>

要了解更多有关:Setting up a Material Components theme for Android

答案 2 :(得分:1)

材质属性颜色 colorControlActivated 发挥了神奇作用。您必须为TextInputLayout创建样式。

例如:

<style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/red</item>
    <item name="colorControlHighlight">@color/blue</item>
</style>

然后,您必须在控件的主题属性中应用此样式:

 <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/lblObservaciones"
                android:theme="@style/TextInputLayoutAppearance"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:boxStrokeColor="@color/blue"
                app:hintTextAppearance="@style/TextAppearance.AppCompat.Medium"
                app:hintTextColor="@color/blue">

            <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/comments"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:gravity="top"
                    android:inputType="textMultiLine"
                    android:maxLength="200" />
</com.google.android.material.textfield.TextInputLayout>