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

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

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

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


当前回答

如果你正在使用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>

其他回答

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

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

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

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变得更小。至少,这是我的案子。

一个小提示是,如果你设置后台为空,并尝试我上面提供的java代码,你的应用程序将在执行后立即崩溃。(因为它得到了背景,但它是空的。)这可能是显而易见的,但我认为指出这一点很重要。

背景设置为空

android:background="@null" in your xml 

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

android:textCursorDrawable="@null"

在您的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"/>