如何在TextInputLayout中更改密码切换的颜色?

时间:2019-04-15 22:19:42

标签: android android-textinputlayout android-selector

如果EditText是否聚焦,我需要更改TextInputLayout中密码切换的颜色。我已经这样做了,但是没有用。颜色始终等于浅灰色(从状态集中=假)

布局

reviews_max

color_password_toggle

ansible_winrm_transport=ntlm

password_toggle_selector

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        app:passwordToggleDrawable="@drawable/password_toggle_selector"
        app:passwordToggleTint="@color/color_password_toggle">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>

4 个答案:

答案 0 :(得分:0)

尝试将color_password_toggle更改为此:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/color_green" android:state_checked="true"  />
<item android:color="@color/color_light_grey" android:state_checked="false" />

您可以删除自定义图标以使用默认的眼睛图标:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        app:passwordToggleTint="@color/color_password_toggle">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>

答案 1 :(得分:0)

我在TextInputLayout中使用了:app:passwordToggleTint =“ @ android:color / black”

答案 2 :(得分:0)

TextInputLayout中包含Material Components library {strong>不推荐使用passwordToggleTint属性(用于密码输入可见性切换的图标)。

现在只需使用 endIconTint 属性。

<com.google.android.material.textfield.TextInputLayout
    ...
    app:endIconMode="password_toggle"
    android:hint="Password"
    style="@style/FilledBoxEndIconTint">

    <com.google.android.material.textfield.TextInputEditText
         ...
         android:inputType="textPassword"/>

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

具有:

  <style name="FilledBoxEndIconTint" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="endIconTint">@color/my_selector_color</item>
  </style>

您可以使用颜色或选择器。

enter image description here

您还可以在布局中使用app:endIconTint属性:

 <com.google.android.material.textfield.TextInputLayout
            ...
            app:endIconMode="password_toggle"
            android:hint="Password"
            app:endIconTint="@color/my_selector_color">

默认选择器是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_activated="true"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.6" android:color="?attr/colorOnSurface"/>
</selector>

答案 3 :(得分:0)

似乎TextInputEditText获得焦点时,并没有将TextInputLayout的状态设置为state_activated

但是,如果创建自己的TextInputEditText版本,则很容易实现。

class MyTextInputEditText : TextInputEditText {
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context) : super(context)

    override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect)
        getTextInputLayout()?.setEndIconActivated(focused)
    }

    //copied from TextInputEditText (why this is private?)
    private fun getTextInputLayout(): TextInputLayout? {
        var parent = parent
        while (parent is View) {
            if (parent is TextInputLayout) {
                return parent
            }
            parent = parent.getParent()
        }
        return null
    }
}

基本上,只需按照@gabriele-mariotti的建议进行操作,创建一个将android:state_activatedandroid:state_enabledandroid:state_checked组合在一起的选择器即可。

我建议对GitHub上的check the PR材料库进行更改。