如何在EditText中隐藏下划线

时间:2012-12-20 15:36:14

标签: android textview android-edittext

如何隐藏EditText下划线(两端带有小衬线的提示行)?

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

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

作为一种权宜之计,我将通过删除EditText并替换TextView来实现这一点。我认为这会产生预期的结果,但这似乎是一种迂回的做法,通过改变属性来做一些应该很容易做的事情。

25 个答案:

答案 0 :(得分:867)

您可以将EditText设置为自定义透明可绘制或仅使用

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

android:background="@null"

答案 1 :(得分:96)

将背景设为null。

android:background="@null"

答案 2 :(得分:25)

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

android:backgroundTint="@color/Transparent"

<color name="Transparent">#00000000</color>

但您可以在Api v21(Lollipop)或更高版本

中使用此功能

答案 3 :(得分:24)

请将您的edittext背景设置为

android:background="#00000000"

它会起作用。

答案 4 :(得分:19)

您可以使用setBackgroundResource

以编程方式执行此操作
editText.setBackgroundResource(android.R.color.transparent);

答案 5 :(得分:14)

使用任一属性:

android:background="@null"

OR

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

让我隐藏了EditText的下划线。

但是,请注意它会导致我在EditText

周围的TextInputLayout出现间距问题

答案 6 :(得分:10)

这是一种隐藏它的方法,不会破坏默认填充:

fun View.setViewBackgroundWithoutResettingPadding(background: Drawable?) {
    val paddingBottom = this.paddingBottom
    val paddingStart = ViewCompat.getPaddingStart(this)
    val paddingEnd = ViewCompat.getPaddingEnd(this)
    val paddingTop = this.paddingTop
    ViewCompat.setBackground(this, background)
    ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
}

用法:

editText.setViewBackgroundWithoutResettingPadding(null)

答案 7 :(得分:9)

我所做的是创建一个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值作为firelds,但为了清晰起见,我在这里简化了形状文件。

答案 8 :(得分:5)

您还必须设置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"
            />

答案 9 :(得分:4)

如果您在 TextInputLayout 中使用 EditText ,请使用// a.ts interface Config { selector?: <T>(httpResponse: HttpResponse<T>) => unknown; } export function factory(options: Config): something { // use options to return something } // another file b.ts // I have a type for res only here(b.ts), and I want to use it to specialize type T import { factory } from 'a.ts'; factory({ selector: res => res.data.data; // error: Property 'data' does not exist on type 'T' }); ,如下所示:

app:boxBackgroundMode="none"

答案 10 :(得分:4)

如果您的编辑文字已有背景,则可以使用以下内容。

android:textCursorDrawable="@null"

答案 11 :(得分:4)

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

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会变小。至少是我的情况。

需要注意的一点是,如果将background设置为null并尝试我在上面提供的Java代码,则您的应用在执行后将立即崩溃。 (因为它有背景,但它为空。)这很明显,但我认为指出这一点很重要。

答案 12 :(得分:3)

如果希望此方法影响EditText的所有实例以及从其继承的任何类,则应在主题中设置属性editTextBackground的值。

  <item name="android:editTextBackground">@drawable/bg_no_underline</item>

我使用的可绘制对象的示例是:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
     android:insetLeft="@dimen/abc_edit_text_inset_horizontal_material"
     android:insetRight="@dimen/abc_edit_text_inset_horizontal_material"
     android:insetTop="@dimen/abc_edit_text_inset_top_material"
     android:insetBottom="@dimen/abc_edit_text_inset_bottom_material">
    <selector>
      <item android:drawable="@android:color/transparent"/>
    </selector>
</inset>

这是默认材料设计实现的略微修改版本。

应用后,它将使您所有的EditText删除整个应用程序中的下划线,并且您不必手动将样式应用于每个样式。

答案 13 :(得分:3)

我发现了最奇怪的事情!如果使用数据绑定将其设置为nullandroid:background="@{null}"

然后不仅删除了背景,而且视图仍然具有从默认背景计算的填充。因此,由于某种原因,延迟空值设置不会清除前一个bg的填充..?视图上的填充为左/上/右4dp,下13dp(来自仿真器级别21)。

所有API级别可能没有相同的最终结果,所以要小心!有人告诉我,如果你测试这个并发现它可靠。 (另请注意,底部填充因为原始下划线而突出。因此您可能希望在XML中更改它,或者在加载到相同顶部之后在代码中重置它...

答案 14 :(得分:2)

您还可以为editText定义STYLE,以便重新组合所有属性。如果您需要多个编辑需要具有行为的文本

,这是非常强大的

将代码放在res / values / styles.xml

<style name="MyEditTextStyle" parent="@android:style/TextAppearance.Widget.EditText">
    <item name="android:background">@color/transparence</item> //NO UNDERBAR
    <item name="android:maxLines">1</item>
    <item name="android:height">28dp</item> //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" />

答案 15 :(得分:2)

仅使用此

 editText.setBackgroundColor(Color.TRANSPARENT);

答案 16 :(得分:1)

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

xml中使用:android:background="@null"

答案 17 :(得分:1)

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

android:background="@null"

答案 18 :(得分:1)

就我而言,我使用自定义背景来编辑文本 所以将背景设置为@null或将色调设置为透明不是我的解决方案 所以我玩了一个小技巧,对我来说很好,我刚设置

android:inputType="textVisiblePassword"

它可以很好地完成工作。 它不是最佳解决方案,但可以工作

答案 19 :(得分:0)

将背景设置为空

android:background="@null" in your xml 

答案 20 :(得分:0)

如果您正在使用背景,则必须使用此标记 'android:testCursorDrawable =“ @ null”'

答案 21 :(得分:0)

就我而言,my_web_project/src/main/webapp/template_dir_name/theme_name/my_field_template.ftl最好。

它不会删除位于栏正下方的默认填充。

editText.setBackgroundResource(R.color.transparent);

答案 22 :(得分:0)

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

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" />

答案 23 :(得分:0)

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

...它不是最佳解决方案,但可以。

答案 24 :(得分:0)

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