ChatGPT解决这个技术问题 Extra ChatGPT

如何在 EditText 中隐藏下划线

如何隐藏 EditText 下栏(末尾带有小衬线的提示行)?

可能有更好的方法来做我想做的事:我有一个带有 EditText 的布局。通常,在用户可以点击它并开始输入或编辑文本的地方,这显示得很好。

但是,有时我想使用相同的布局(简化其他逻辑)以只读方式显示相同的数据。我希望演示文稿相似 - 它应该具有相同的高度和相同的字体,但没有下划线。

作为权宜之计,我将通过删除 EditText 并替换 TextView 来实现这一点。我认为这会产生预期的结果,但它似乎是一种迂回,一种昂贵的方式来做一些应该很容易通过改变属性来完成的事情。


m
murgupluoglu

您可以将 EditText 设置为具有自定义透明可绘制对象或仅使用

android:background="@android:color/transparent"

或者

android:background="@null"

或以编程方式

editText.setBackgroundResource(android.R.color.transparent);

正如 ha1ogen 所说,最好的答案是制作自定义可绘制对象。从用于普通 EditText 字段的 9-patch 开始。修改它以去除您不想要的下划线和其他图形。这样,您修改后的 EditText 将具有与普通 EditText 字段相同的边距和整体外观。如果您只是将背景设置为空,它将失去边距。
使用这个:android:background="@android:color/transparent"
如果我已经有其他颜色的背景,比如说灰色,在这种情况下如何删除下划线/下划线?
在 Android 19 中,使用 android:background="@android:color/transparent" 会导致相同的边距损失问题...是否有任何人创建此类自定义可绘制对象的示例?
我们如何在 Java 中以编程方式进行操作?
J
Jurgo

将背景设置为空。

android:background="@null"

这与 background="#00000000" 相同。真的不行。
这对我有用,但我最终做了 android:background="@android:color/transparent,因为 @null 很可怕。
@Richard 为什么 @null 很可怕?
“@null”也隐藏了闪烁的光标,“@android:color/transparent”没有。
@VinayakVNaik 实际上 #00000000 是透明的黑色。不是透明的白色,即#00ffffff
Y
Yasin Kaçmaz

您可以将 EditTextbackgroundTint 值设置为特定颜色。如果您设置透明颜色,下划线应该消失。

android:backgroundTint="@color/Transparent"

<color name="Transparent">#00000000</color>

但您可以在 Api v21(Lollipop) 或更高版本中使用它


这比改变整个视图的背景要好得多(正如其他答案所暗示的)
它还保留了边距,因为它不会删除背景本身,只是将其颜色更改为透明,所以我选择了这个
J
Jared Burrows

请将您的edittext背景设置为

android:background="#00000000"

它会起作用的。


它会有点工作,但不正确。如果您只是删除背景,您还将丢失字段周围的边距 - 它们是默认背景的一部分。正如@ha1ogen 所建议的那样,正确的解决方案是替换一个根本没有下划线的不同背景
如何以编程方式撤消此操作
Z
Zoe stands with Ukraine

您可以使用 setBackgroundResource 以编程方式执行此操作:

editText.setBackgroundResource(android.R.color.transparent);

我不知道为什么,但它不起作用。线路还在
P
Paul LeBeau

我所做的是创建一个 Shape 可绘制对象并将其设置为背景:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <padding
        android:top="8dp"
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp" />

    <solid android:color="#fff" />

</shape>

注意:我实际上使用 @dimen@color 值作为 firelds,但为了清楚起见,我在此处简化了 shape 文件。


这也是一个非常好的选择。这是设置自定义可绘制对象的一种变体。
T
ThePierrasse

使用任一属性:

android:background="@null"

或者

android:background="@android:color/transparent"

为我工作以隐藏 EditText 的下划线。

但是,请注意,它会导致我围绕 EditText 的 TextInputLayout 出现间距问题


间距问题来自错误布局。为了解决这个问题,将 TextInputLayout 的 errorEnabled 属性设置为 false app:errorEnabled="false"
D
Darush

如果您在 TextInputLayout 中使用 EditText,请使用 app:boxBackgroundMode="none",如下所示:

<com.google.android.material.textfield.TextInputLayout
    app:boxBackgroundMode="none"
    ...
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

t
tir38

这是一种隐藏它的方法,不会破坏默认填充:

fun View.setViewBackgroundWithoutResettingPadding(background: Drawable?) {
    val paddingBottom = this.paddingBottom
    val paddingStart = ViewCompat.getPaddingStart(this)
    val paddingEnd = ViewCompat.getPaddingEnd(this)
    val paddingTop = this.paddingTop
    ViewCompat.setBackground(this, background)
    ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
}

用法:

editText.setViewBackgroundWithoutResettingPadding(null)

更新:

如果你发现自己总是传递 null,你可以在方法中编写它(然后你也可以重载 EditText 本身)

fun EditText.removeUnderline() {
    val paddingBottom = this.paddingBottom
    val paddingStart = ViewCompat.getPaddingStart(this)
    val paddingEnd = ViewCompat.getPaddingEnd(this)
    val paddingTop = this.paddingTop
    ViewCompat.setBackground(this, null)
    ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
}

// usage:
editText.removeUnderline()

这个答案是完美的,应该被接受。它可以在不删除默认填充的情况下删除背景。
H
Harvinder Singh

在我的情况下,我使用自定义背景来编辑文本,因此将背景设置为 @null 或将色调设置为透明不是我的解决方案,所以我玩了一个小技巧,它对我非常有效,我刚刚设置

android:inputType="textVisiblePassword"

它可以很好地完成工作..它不是最佳解决方案,但它有效


我喜欢与透明背景结合使用,因为它隐藏了打字时显示在文本下方的栏 - 我想要的只是文本,没有装饰。
这个答案是唯一对我有用的答案,因为我的编辑文本框是以编程方式创建的,并且每个都有不同的背景颜色,因此背景不能为空并依赖于父背景颜色
l
live-love

您还必须设置 minWidth,否则如果文本为空,光标将消失。

        <EditText
            android:id="@+id/et_card_view_list_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="30dp"
            android:layout_weight="1"
            android:inputType="text"
            android:text="Name"
            android:background="@android:color/transparent"
            />

b
bengongon97

我有这样的东西非常有用:

generalEditText.getBackground().mutate().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);

其中generalEditText 是我的EditText,白色是:

<color name="white">#ffffff</color>

这不会删除填充,您的 EditText 将保持原样。只有底部的行将被删除。有时这样做更有用。

如果您使用 android:background="@null" 的建议数量,您会丢失填充,并且 EditText 会变小。至少,这是我的情况。

需要注意的是,如果您将背景设置为 null 并尝试我上面提供的 java 代码,您的应用程序将在执行后立即崩溃。 (因为它有背景但它是空的。)这可能很明显,但我认为指出它很重要。


p
prashant kute

只需使用这个

 editText.setBackgroundColor(Color.TRANSPARENT);

a
androidguy

我发现了最奇怪的事情!如果您使用数据绑定将其设置为 nullandroid:background="@{null}"

然后不仅背景被删除,而且视图仍然具有从默认背景计算的填充。所以由于某种原因,延迟的 null 设置不会清除前一个 bg 中的填充..?视图上的填充是左/上/右 4dp,底部 13dp(来自模拟器级别 21)。

在所有 API 级别上可能不会有相同的最终结果,所以要小心!有人告诉我您是否对此进行了测试并发现它可靠。 (另请注意,底部填充由于原始中的下划线而突出。所以您可能希望在 XML 中更改它,或者在加载到等于顶部后在代码中重置它......


A
AKASH WANGALWAR

如果您的编辑文本已经有背景,那么您可以使用以下。

android:textCursorDrawable="@null"

虽然这并不能解决问题,但它对我有帮助。
k
kingargyle

如果您希望这影响 EditText 的所有实例以及从它继承的任何类,那么您应该在主题中设置属性 editTextBackground 的值。

  <item name="android:editTextBackground">@drawable/bg_no_underline</item>

我使用的drawable的一个例子是:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
     android:insetLeft="@dimen/abc_edit_text_inset_horizontal_material"
     android:insetRight="@dimen/abc_edit_text_inset_horizontal_material"
     android:insetTop="@dimen/abc_edit_text_inset_top_material"
     android:insetBottom="@dimen/abc_edit_text_inset_bottom_material">
    <selector>
      <item android:drawable="@android:color/transparent"/>
    </selector>
</inset>

这是默认材料设计实现的略微修改版本。

应用后,它将使您的所有 EditText 删除整个应用程序中的下划线,并且您不必手动将样式应用于每一个。


R
Rehan Khan

在您的 XML Edittext 中使用此代码

android:background="@android:color/transparent"

如下代码所示

         <EditText
                    android:id="@+id/EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"/>

A
Andreas
android:background="@android:color/transparent"

或者

android:background="@null"

T
Twinkle

您还可以为您的 editText 定义一个样式,以便您可以重新组合所有共同的属性。如果您必须多次编辑需要具有该行为的文本,它非常强大

将代码放入 res/values/styles.xml

之后你只需要在你的editText中调用它

<EditText
    android:id="@+id/edit1"
    style="@style/MyEditTextStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/edit2"
    style="@style/MyEditTextStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

R
RAGHHURAAMM

以编程方式使用:editText.setBackground(null)

xml 使用:android:background="@null"


Z
Zain
android:inputType="textVisiblePassword|textMultiLine"
android:background="@android:color/transparent"

...它不是最佳解决方案,但它有效。


B
Book Of Zeus

将背景设置为空

android:background="@null" in your xml 

H
HJ Song

就我而言,editText.setBackgroundResource(R.color.transparent); 是最好的。

它不会删除默认填充,就在 bar 下。

R.color.transparent = #00000000


L
Lakhwinder Singh

如果您使用背景,那么您必须使用此标签

android:testCursorDrawable="@null" 

i
itabdullah

要保留边距和背景颜色,请使用:

背景.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <padding
        android:bottom="10dp"
        android:left="4dp"
        android:right="8dp"
        android:top="10dp" />

    <solid android:color="@android:color/transparent" />

</shape>

编辑文本:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/none_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:inputType="text"
    android:text="First Name And Last Name"
    android:textSize="18sp" />

f
fcdt

另一种选择是,您可以像这样创建自己的自定义 EditText:

class CustomEditText : androidx.appcompat.widget.AppCompatEditText {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private val paint = Paint()
    private val path = Path()

    init { // hide your underbar
        this.setBackgroundResource(android.R.color.transparent)

        // other init stuff...
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        // draw your canvas...
    }
}

С
Саша Мазур

对于固定提示使用这个

        <item name="boxStrokeWidth">0dp</item>
        <item name="boxCornerRadiusTopStart">12dp</item>
        <item name="boxCornerRadiusTopEnd">12dp</item>
        <item name="boxCornerRadiusBottomStart">12dp</item>
        <item name="boxCornerRadiusBottomEnd">12dp</item>