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

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

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

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


当前回答

您还必须设置minWidth,否则如果文本为空,光标将消失。

        <EditText
            android:id="@+id/et_card_view_list_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="30dp"
            android:layout_weight="1"
            android:inputType="text"
            android:text="Name"
            android:background="@android:color/transparent"
            />

其他回答

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

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

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

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

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

or

android:background="@null"

或通过编程的方式

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

将background设置为null。

android:background="@null"

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

android:background="@null"