TextInputLayout изменить цвет значка запуска в фокусе

Как изменить цвет startIconDrawable, когда фокус находится на TextInputEditText

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:layoutDirection = "rtl"
            app:boxBackgroundColor = "@color/white"
            app:endIconMode = "clear_text"
            app:startIconDrawable = "@drawable/person_24dp">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:gravity = "right"
                android:hint = "family"
                android:imeOptions = "actionDone"
                android:singleLine = "true"
                android:textSize = "@dimen/mediumTextSize" />
        </com.google.android.material.textfield.TextInputLayout>

2
0
2 284
3
Перейти к ответу Данный вопрос помечен как решенный

Ответы 3

Ответ принят как подходящий

Котлин:

textInputEditText.setOnFocusChangeListener { _, hasFocus ->
    val color = if (hasFocus) Color.BLUE else Color.GRAY
    textInputLayout.setStartIconTintList(ColorStateList.valueOf(color))
}

Джава:

textInputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        int color = hasFocus ? Color.BLUE : Color.GRAY;
        textInputLayout.setStartIconTintList(ColorStateList.valueOf(color));
    }
});

Код Java можно упростить с помощью лямбда-выражения.

Вы можете решить это BindingAdapter

@BindingAdapter("startIconTintOnFocus")
fun TextInputLayout.startIconTintOnFocus(textInputEditTextId: Int) {
    val et = findViewById<TextInputEditText>(textInputEditTextId)
    et.setOnFocusChangeListener { _, hasFocus ->
        val color = if (hasFocus) ContextCompat.getColor(context, R.color.colorAccent) else ContextCompat.getColor(context, R.color.red)
        this.setStartIconTintList(ColorStateList.valueOf(color))
    }
}

установите startIconTintOnFocus в TextInputLayout и отправьте ему идентификатор TextInputEditText

<com.google.android.material.textfield.TextInputLayout
    android:id = "@+id/outlinedTextField"
    style = "@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    app:startIconTint = "@color/red"
    startIconTintOnFocus = "@{@id/textInputEditText}"
    app:startIconDrawable = "@drawable/ic_phone_android_24dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id = "@+id/textInputEditText"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:textColor = "@color/textColorPrimary"
        android:textSize = "@dimen/mediumTextSize"/>

</com.google.android.material.textfield.TextInputLayout>

спасибо @miel3k

Решение с XML

Создайте селектор цвета для startIconTint или endIconTint:

<selector xmlns:android = "http://schemas.android.com/apk/res/android">
    <item android:color = "@color/text" android:state_focused = "true"/>
    <item android:color = "@color/input" android:state_focused = "false"/>
</selector>

А для TextInputLayout установите startIconTint или endIconTint:

Например:

<com.google.android.material.textfield.TextInputLayout
    android:id = "@+id/passwordLayout"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:hint = "@string/login_edit_password"
    app:endIconTint = "@color/YOUR_CREATED_COLOR_SELECTOR"
    app:endIconMode = "password_toggle">

    <com.google.android.material.textfield.TextInputEditText
        android:id = "@+id/password"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:inputType = "textPassword"
        tools:ignore = "TextContrastCheck" />
</com.google.android.material.textfield.TextInputLayout>

Без фокуса:

С фокусом:

Другие вопросы по теме