我如何隐藏EditText下划线(提示行与小衬线在结束)?

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

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

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


当前回答

请将编辑文本背景设置为

android:背景= " # 00000000 "

它会起作用的。

其他回答

你可以通过编程方式使用setBackgroundResource:

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

将background设置为null。

android:background="@null"

简单使用

 editText.setBackgroundColor(Color.TRANSPARENT);

这里有一个隐藏它的方法,而不破坏默认的填充:

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()

在我的例子中,editText.setBackgroundResource(R.color.transparent);是最好的。

它不删除默认填充,只是在栏下。

R.color.transparent = #00000000