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

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

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

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


当前回答

我所做的是创建一个Shape drawable,并将其设置为背景:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <padding
        android:top="8dp"
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp" />

    <solid android:color="#fff" />

</shape>

注意:我实际上使用的是@dimen和@color值,但为了清晰起见,我简化了形状文件。

其他回答

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

要同时保留页边距和背景色,请使用:

background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <padding
        android:bottom="10dp"
        android:left="4dp"
        android:right="8dp"
        android:top="10dp" />

    <solid android:color="@android:color/transparent" />

</shape>

编辑文本:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/none_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:inputType="text"
    android:text="First Name And Last Name"
    android:textSize="18sp" />

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

android:inputType="textVisiblePassword"

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

请将编辑文本背景设置为

android:背景= " # 00000000 "

它会起作用的。

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

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代码,你的应用程序将在执行后立即崩溃。(因为它得到了背景,但它是空的。)这可能是显而易见的,但我认为指出这一点很重要。