我如何隐藏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"
            />

其他回答

还可以为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" />

您可以将EditText的backgroundTint值设置为特定的颜色。 如果你设置透明颜色,下划线应该消失。

android: backgroundTint = @color - Transparent”

< [name = Transparent”> # 00000000 < /颜色>

但是你可以在Api v21(Lollipop)或更高版本中使用它

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

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

android:inputType="textVisiblePassword"

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

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

android:testCursorDrawable="@null"