如何在TextInputLayout的密码切换按钮上禁用涟漪效果

5
我使用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" - Gabriele Mariotti
发布您正在尝试实现的布局。 - Gabriele Mariotti
移除 android:background 并尝试一次。 - Avilash
我需要表单的背景来保持其外观,因此无法将其移除。 - Bagus Aji Santoso
3个回答

2
这不是最优解决方案,但对我有效。
materialVersion: 1.1.0

Kotlin:

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

R.id.text_input_end_icon 是通过布局检查器找到的。


1
下面是创建密码切换新主题的简单方法。父主题是您的主要(App)主题。
<style name="PasswordToggleTransparent" parent="NoActionBar">
        <item name="colorControlHighlight">#0000</item>
</style>

将这个主题应用到你的 TextInputLayout:
android:theme="@style/PasswordToggleTransparent"

这个可以工作,但只有当应用为android:theme而不是样式时才能工作。 - Bruce

1
你可以通过在 TextInputEditText 中删除 android:background="@drawable/bg_edit" 并使用 OutlinedBox 样式 来实现相同的效果。
    <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样式,但它不能直接使用。这是聚焦前的外观https://imgur.com/nhlGIeB,聚焦EditText后的外观为https://imgur.com/nUVTMCC。 - Bagus Aji Santoso
@BagusAjiSantoso 不是很清楚。您需要在TextInputEditText中删除背景。 - Gabriele Mariotti
是的,我已经去除了背景,但OutlinedBox看起来对齐得不好。除了样式、约束和宽度/高度之外,我没有其他属性。 - Bagus Aji Santoso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接