更改 EditText 控件在选定状态下的颜色

Vic*_*ich 5 android android-edittext android-styles

在我的应用程序中,我从服务器获取应用程序的配色方案。我根据服务器设置更新所有应用程序视图的颜色。我的问题与更新选定状态下 EditText 的颜色有关:

我可以使用该方法更改选择的背景颜色,但是我可以以某种方式更改左下角和右下角的蓝色拖动视图editText.setHighlightColor()的颜色吗?我无法使用样式属性更改颜色,因为我在运行时从服务器获取颜色。我可以动态更改拖动视图吗?colorAccent

Đứ*_*ông 0

这是完整的示例,自定义 TextInputLayout 颜色:

在您的自定义*.xml文件中,检查属性styleandroid:theme

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/txtConfirmPassword"
    style="@style/TextInputLayoutStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="9dp"
    android:hint="@string/confirm_password_hint"
    android:importantForAutofill="no"
    app:hintAnimationEnabled="false"
    app:hintEnabled="false"
    app:passwordToggleEnabled="true">

    <com.google.android.material.textfield.TextInputEditText
        style="@style/TextInputEditTextStyle"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:hint="@string/password_hint"
        android:inputType="textPassword"
        android:singleLine="true"
        android:theme="@style/TextInputEditTextTheme" />
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

在你的themes.xml文件中

<style name="TextInputEditTextStyle">
    <item name="android:textColor">@color/main_text</item>
</style>

<style name="TextInputEditTextTheme">
    <item name="android:textColorHighlight">@color/gray2</item> <!-- background of selected text -->
    <item name="android:colorControlActivated">@color/main_text</item> <!-- cursor -->
</style>
Run Code Online (Sandbox Code Playgroud)

您可以自定义TextInputLayout边框,将其添加到themes.xml

    <style name="TextInputLayoutStyle" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="boxStrokeColor">@color/text_input_layout_tint</item>
    <item name="boxStrokeWidth">1dp</item>
    <item name="boxStrokeWidthFocused">1dp</item>
    <item name="boxCornerRadiusBottomEnd">8dp</item>
    <item name="boxCornerRadiusBottomStart">8dp</item>
    <item name="boxCornerRadiusTopStart">8dp</item>
    <item name="boxCornerRadiusTopEnd">8dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

文件@color/text_input_layout_tint

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/action_yes" android:state_focused="true" />
    <item android:color="@color/stroke" />
</selector>
Run Code Online (Sandbox Code Playgroud)