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

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

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

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


当前回答

在我的例子中,editText.setBackgroundResource(R.color.transparent);是最好的。

它不删除默认填充,只是在栏下。

R.color.transparent = #00000000

其他回答

在editText Android Studio中删除下划线的几种方法

android:背景= " @null " 上面的代码在某些方面产生了一个问题 或

使用android:背景= " @android:颜色/透明”

**或程序化的**

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

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

让我们在android中做一些更简单的事情,比如outlinedbox和Null background of edittext。如下所示。

复制粘贴即可。

编辑文本与OutlinedBox密码与toogle选项在结束。

    <com.google.android.material.textfield.TextInputLayout    
          style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="@string/password"
          app:boxCornerRadiusBottomEnd="10dp"
          app:boxCornerRadiusBottomStart="10dp"
          app:boxCornerRadiusTopEnd="10dp"
          app:boxCornerRadiusTopStart="10dp"
          app:boxStrokeColor="@color/primary_color"
          app:boxStrokeWidth="1dp"
          app:boxStrokeWidthFocused="1dp"
          app:errorIconDrawable="@null"
          app:hintTextColor="#d3d3d3"
          app:passwordToggleDrawable="@drawable/password_toggle"
          app:passwordToggleEnabled="true">
    
          <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:paddingStart="10dp"
                android:paddingEnd="0dp"
                android:textCursorDrawable="@null"
                android:textSize="12sp" />
</com.google.android.material.textfield.TextInputLayout>

为password_toggle创建Drawble

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_show" android:state_checked="true"/>
    <item android:drawable="@drawable/ic_hide"/>
</selector>

现在没有任何边界的编辑文本。

电子邮件编辑文本

<com.google.android.material.textfield.TextInputEditText
       android:id="@+id/email"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/bg_edittext"
       android:hint="@string/enter_your_email"
       android:imeOptions="actionDone"
       android:inputType="textEmailAddress"
       android:maxLines="1"
       android:padding="10dp"
       android:singleLine="true"
       android:textColor="@color/black"
       android:textColorHint="#d3d3d3"
       android:textSize="12sp" />

对于密码编辑文本。

<com.google.android.material.textfield.TextInputEditText
       android:id="@+id/password"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/bg_edittext"
       android:hint="@string/enter_your_password"
       android:imeOptions="actionDone"
       android:inputType="textEmailAddress"
       android:maxLines="1"
       android:padding="10dp"
       android:singleLine="true"
       android:textColor="@color/black"
       android:textColorHint="#d3d3d3"
       android:textSize="12sp" />

简单使用

 editText.setBackgroundColor(Color.TRANSPARENT);

我发现了一件最奇怪的事!如果使用数据绑定将其设置为null: android:背景= " @{零}"

然后,不仅背景被删除,而且视图仍然有从默认背景计算的填充。所以出于某种原因,延迟空设置不清除填充从以前的bg..?视图上的填充是左/上/右4dp,下13dp(来自模拟器级别21)。

在所有API级别上可能不会有相同的最终结果,所以要小心!谁能告诉我你是否测试过这个方法是否可靠。(还要注意,底部填充突出是因为原来的下划线。所以你可能想要在XML中改变它,或者在代码中重置它,在它加载到equal top…