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

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

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

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


当前回答

您可以将EditText设置为具有自定义的透明绘图对象,或者直接使用

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

or

android:background="@null"

或通过编程的方式

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

其他回答

还可以为editText定义一个STYLE,这样就可以重新组合所有属性。这是非常强大的,如果你有多个编辑文本,需要有行为

将代码放在res/values/styles.xml中

<样式 <项目=“android:背景”>@color/透明</项目> //NO UNDERBAR < name = " android项目:maxLines " > 1 < - >项目 <项目=“android:height”>28dp</项目> //COMMON height < / style >

之后,您只需要在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" />

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

android:textCursorDrawable="@null"

另一个选择,你可以创建自己的自定义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...
    }
}

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

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

我所做的是创建一个Shape drawable,并将其设置为背景:

<?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值,但为了清晰起见,我简化了形状文件。