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

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

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

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


当前回答

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

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

or

android:background="@null"

或通过编程的方式

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

其他回答

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

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

R.color.transparent = #00000000

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

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

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

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

android:background="@null"

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

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

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

**或程序化的**

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