【问题标题】:How to disable ripple effect on password toggle button of TextInputLayout如何在 TextInputLayout 的密码切换按钮上禁用涟漪效应
【发布时间】:2019-10-03 16:44:30
【问题描述】:

我使用 TextInputLayout 来显示密码切换按钮。它正在工作,但涟漪效果在 EditText 的背景后面(我为 EditText 使用可绘制背景)。如何禁用密码按钮的波纹效果或将波纹置于 EditText 背景前面?这里是演示问题https://imgur.com/nYOB6Ye 的录制视频。

<com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                app:hintEnabled="false"
                app:passwordToggleEnabled="true">

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/bg_edit"
                    android:hint="••••••"
                    android:inputType="textPassword"
                    android:padding="18dp" />
            </com.google.android.material.textfield.TextInputLayout>

【问题讨论】:

  • 你为什么在你的EditText中使用android:background="@drawable/bg_edit"
  • 发布您想要实现的布局。
  • 移除 android:background 并尝试一次
  • 我需要背景作为表单的外观,所以我无法删除它。

标签: android xml material-design android-textinputlayout material-components-android


【解决方案1】:

这不是出色的解决方案,但对我有用。
材料版本:1.1.0

科尔廷:

textInputLayout.apply {
        findViewById<View>(R.id.text_input_end_icon).setBackgroundColor(
            ResourcesCompat.getColor(resources, R.color.transparent, theme)
        )
    }

R.id.text_input_end_icon 是通过 Layout Inspector 找到的

【讨论】:

    【解决方案2】:

    一种简单的方法,为密码切换创建一个新主题如下所示。父主题是您的主要(应用)主题。

    <style name="PasswordToggleTransparent" parent="NoActionBar">
            <item name="colorControlHighlight">#0000</item>
    </style>
    

    并将这个主题应用到您的 TextInputLayout

    android:theme="@style/PasswordToggleTransparent"
    

    【讨论】:

    • 这行得通,不知何故,它只在应用为​​ android:theme 而不是样式时才有效
    【解决方案3】:

    删除TextInputEditText 中的android:background="@drawable/bg_edit" 并使用OutlinedBox style 可以获得相同的结果:

        <com.google.android.material.textfield.TextInputLayout
            android:hint="••••••"
            app:endIconMode="password_toggle"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            app:boxBackgroundColor="@color/....."
            ..>
    
               <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"
                    android:padding="18dp" />
    
        </com.google.android.material.textfield.TextInputLayout>
    

    注意:app:passwordToggleEnabled="true" 已弃用。只需添加app:endIconMode="password_toggle"

    【讨论】:

    • 我尝试过 OutlinedBox 样式,但它不能开箱即用。这是焦点imgur.com/nhlGIeB 之前的外观,这里是编辑文本imgur.com/nUVTMCC 的焦点之后的样子。
    • @BagusAjiSantoso 目前尚不清楚。您必须删除 TextInputEditText 中的背景。
    • 是的,我已经删除了背景,但是 OutlinedBox 看起来没有很好地对齐。除了样式、约束和宽度/高度之外,我没有其他属性。
    猜你喜欢
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多