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

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

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

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


当前回答

如果你正在使用background,那么你必须使用这个标签

android:testCursorDrawable="@null" 

其他回答

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

android:inputType="textVisiblePassword"

而且它能很好地完成工作。 这不是最佳解决方案,但它有效

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

使用任一属性:

android:background="@null"

OR

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

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

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

在editText Android Studio中删除下划线的几种方法

android:背景= " @null " 上面的代码在某些方面产生了一个问题 或

使用android:背景= " @android:颜色/透明”

**或程序化的**

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

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

android:background="@null"