从 TextInputEditText 中删除下划线

Remove underline from TextInputEditText

我有一个 android 屏幕可以接收用户的电子邮件。下面是代码片段,我想删除出现在文本下方的下划线。

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:hint="@string/email"
    android:textColorHint="@color/blueBackground"
    app:boxBackgroundColor="@color/input_background"
    app:boxCornerRadiusBottomEnd="20dp"
    app:boxCornerRadiusBottomStart="20dp"
    app:boxCornerRadiusTopEnd="20dp"
    app:boxCornerRadiusTopStart="20dp"
    app:endIconMode="clear_text"
    app:endIconTint="@color/blueBackground"
    app:hintTextColor="@color/blueBackground">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/blueBackground"
        android:textSize="18sp"
        android:layout_margin="8dp" />

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

我试过android:background="@null"

<item name="colorControlNormal">@android:color/transparent</item> <item name="colorControlActivated">@android:color/transparent</item>

但它不起作用。

尝试将背景设置为 @null@android:color/transparent

看起来 Material 组件库使用 app:boxStrokeColor 绘制了自己的下划线。此属性是 ColorStateList,因此您必须创建一个颜色状态列表资源,其中所有状态的颜色都设置为透明。所以基本上你想创建一个新文件 res/color/filename.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:color="@android:color/transparent"
        android:state_pressed="true"
        android:state_focused="true"
        android:state_selected="true"
        android:state_checkable="true"
        android:state_checked="true"
        android:state_enabled="true"
        android:state_window_focused="true" />
</selector>

并将 app:boxStrokeColor 属性设置为 @color/filename

然而,这给我留下了一条黑色下划线,它仍然没有响应 android:background=@null<item name="colorControl*">@android:color/transparent</item>


TL;DR

Quickfix:如果您使用 style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" 设置 TextInputLayout 轮廓,框看起来几乎相同,但下划线消失了。为了删除笔画,只需将 app:boxStrokeColor 属性设置为 @null.

将 TextInputLayout 的背景设置为可绘制对象并将 TextInputEditText 之一设置为透明以删除下划线:

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:hint="@string/email"
        android:background="@drawable/blue_drawable"
        app:endIconMode="clear_text"
        app:endIconTint="@color/black"
        app:hintTextColor="@color/black">

    <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:background="@android:color/transparent"
            android:textSize="18sp"
            android:layout_margin="8dp" />

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

blue_drawable.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<corners android:radius="20dp"/>
<solid android:color="@android:color/holo_blue_bright"/></shape>

res/drawable

创建一个选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="0.01" android:color="?attr/background" android:state_focused="true"/>
    <item android:alpha="0.01" android:color="?attr/background" android:state_hovered="true"/>
    <item android:alpha="0.01" android:color="?attr/background" android:state_enabled="false"/>
    <item android:alpha="0.01" android:color="?attr/background"/>
</selector>

背景 这里 - 任何接近你背景的颜色。

方式一 然后将其设置为您的 TextInputLayout:

<com.google.android.material.textfield.TextInputLayout
...
  app:boxStrokeColor="@drawable/text_input_selector"
...

方式 2,槽型:

styles.xml:

<style name="vm_textFilledBox" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
        <item name="boxStrokeColor">@drawable/text_input_selector</item>
</style>
 <com.google.android.material.textfield.TextInputLayout
...
    style="@style/vm_textFilledBox"

像这样制作您的自定义 TextInputLayout

package com.google.android.material.textfield

import android.content.Context
import android.util.AttributeSet
class TextInputLayoutWithoutUnderline @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {

    override fun updateEditTextBackground() {
        // XXX don't call super on purpose
    }

}
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="textInputStyle">@style/Widget.MyApp.TextInputLayout</item>
</style>

<style name="Widget.MyApp.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="boxStrokeWidth">0dp</item>
    <item name="boxStrokeWidthFocused">0dp</item>
</style>

简单的方法;

app:boxBackgroundMode="filled"
app:boxStrokeWidth="0dp"

app:boxBackgroundMode="none" 在父 TextInputLayout

TextInputLayout 上添加这一行对我有用:

app:boxStrokeWidth="0dp"

更新

在 color res 目录中添加 et_box_color.xml 文件并在其中添加以下行:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:color="@color/transparent"
        android:state_pressed="true"
        android:state_focused="true"
        android:state_selected="true"
        android:state_checkable="true"
        android:state_checked="true"
        android:state_enabled="true"
        android:state_window_focused="true"/>
</selector>

现在添加您的 Material 编辑文本如下:

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/border"
                app:boxStrokeColor="@color/et_box_color"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/llBank">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etAmount"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="1dp"
                    android:background="#00FFFFFF"
                    android:gravity="center"
                    android:hint="Amount"
                    android:imeOptions="actionDone"
                    android:inputType="numberDecimal" />

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

我添加了一个背景来制作 TextInputLayout 的边框,如果不需要,请删除 TextInputLayout 的背景。需要 TextInputEditText 背景才能使背景透明。

If you want app:boxBackgroundMode with filled and without underline than this will work

<item name="boxStrokeWidthFocused">0dp</item>
<item name="boxStrokeWidth">0dp</item>

如果您想使用填充框,那么下面的代码非常适合我。

app:boxStrokeWidth="0dp"
app:boxStrokeWidthFocused="0dp"

在输入布局中添加以上行。

请在TextInputLayout 中设置boxBackgroundMode。这是在 TextInputLayout

中删除下划线的简单而正确的方法
<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:boxBackgroundMode="none" >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/inputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

注意:此答案关于在输入 TextInputEditText

时删除下划线

在搜索了 6 个小时关于如何从 TextInputEditText 中删除下划线之后,我找到了一个单行解决方案,它只是将 android:inputType="text|textNoSuggestions" 放在 TextInputEditText.

现在我在输入 TextInputEditText

时没有看到任何下划线

解决方案很简单,你必须将这一行添加到 TextInputLayoutstyle="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" 它将被修复。

你只需要写 app:boxStrokeWidth="0dp"app:boxStrokeWidthFocused="0dp" TextInputLayout 组件的属性。

应该这样写:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:boxStrokeWidth="0dp"
    app:boxStrokeWidthFocused="0dp"
    android:hint="@string/email"
    android:textColorHint="@color/blueBackground"
    app:boxBackgroundColor="@color/input_background"
    app:boxCornerRadiusBottomEnd="20dp"
    app:boxCornerRadiusBottomStart="20dp"
    app:boxCornerRadiusTopEnd="20dp"
    app:boxCornerRadiusTopStart="20dp"
    app:endIconMode="clear_text"
    app:endIconTint="@color/blueBackground"
    app:hintTextColor="@color/blueBackground">

<com.google.android.material.textfield.TextInputEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/blueBackground"
    android:textSize="18sp"
    android:layout_margin="8dp" />

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