【问题标题】:Remove underline from TextInputEditText从 TextInputEditText 中删除下划线
【发布时间】:2019-11-25 13:25:55
【问题描述】:

我有一个从用户那里接收电子邮件的安卓屏幕。下面是代码的 sn-p,我想删除出现在文本下方的下划线。

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:hint="@string/email"
    android:textColorHint="@color/blueBackground"
    app:boxBackgroundColor="@color/input_background"
    app:boxCornerRadiusBottomEnd="20dp"
    app:boxCornerRadiusBottomStart="20dp"
    app:boxCornerRadiusTopEnd="20dp"
    app:boxCornerRadiusTopStart="20dp"
    app:endIconMode="clear_text"
    app:endIconTint="@color/blueBackground"
    app:hintTextColor="@color/blueBackground">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/blueBackground"
        android:textSize="18sp"
        android:layout_margin="8dp" />

</com.google.android.material.textfield.TextInputLayout>

我试过android:background="@null"

<item name="colorControlNormal">@android:color/transparent</item> <item name="colorControlActivated">@android:color/transparent</item>

但它不起作用。

【问题讨论】:

  • 另外,你可以试试app:boxStrokeWidth="0dp"

标签: android android-textinputlayout android-textinputedittext


【解决方案1】:

解决方法很简单,你必须在TextInputLayout 中加入这一行: style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" 它会被修复。

【讨论】:

    【解决方案2】:

    注意:这个关于在输入TextInputEditText时删除下划线的答案

    在搜索了 6 小时关于如何从 TextInputEditText 中删除下划线后,我找到了一个单行解决方案,只需将 android:inputType="text|textNoSuggestions" 放在您的 TextInputEditText 中。

    现在我在输入 TextInputEditText 时没有任何下划线

    【讨论】:

      【解决方案3】:

      请在 TextInputLayout 中设置 boxBackgroundMode。这是在TextInputLayout中删除下划线的简单而正确的方法

      <com.google.android.material.textfield.TextInputLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:boxBackgroundMode="none" >
      
              <com.google.android.material.textfield.TextInputEditText
                  android:id="@+id/inputEditText"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"/>
      
      </com.google.android.material.textfield.TextInputLayout>
      

      【讨论】:

      • 工作解决方案
      【解决方案4】:

      如果您想使用填充框,那么下面的代码非常适合我。

      app:boxStrokeWidth="0dp"
      app:boxStrokeWidthFocused="0dp"
      

      在输入布局中添加以上行。

      【讨论】:

        【解决方案5】:

        TextInputLayout 上添加这一行对我有用:

        app:boxStrokeWidth="0dp"
        

        更新

        在 color res 目录中添加 et_box_color.xml 文件并在其中添加以下行:

        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
           <item android:color="@color/transparent"
                android:state_pressed="true"
                android:state_focused="true"
                android:state_selected="true"
                android:state_checkable="true"
                android:state_checked="true"
                android:state_enabled="true"
                android:state_window_focused="true"/>
        </selector>
        

        现在添加您的材质编辑文本,如下所示:

        <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/textInputLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/border"
                        app:boxStrokeColor="@color/et_box_color"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/llBank">
        
                        <com.google.android.material.textfield.TextInputEditText
                            android:id="@+id/etAmount"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="1dp"
                            android:background="#00FFFFFF"
                            android:gravity="center"
                            android:hint="Amount"
                            android:imeOptions="actionDone"
                            android:inputType="numberDecimal" />
        
        </com.google.android.material.textfield.TextInputLayout>
        

        我添加了背景来制作 TextInputLayout 的边框,如果不需要,请删除 TextInputLayout 的背景。 TextInputEditText 的背景是使背景透明所必需的。

        【讨论】:

          【解决方案6】:

          如果您希望 app:boxBackgroundMode 带有填充且不带下划线,那么这将起作用

          <item name="boxStrokeWidthFocused">0dp</item>
          <item name="boxStrokeWidth">0dp</item>
          

          【讨论】:

            【解决方案7】:

            app:boxBackgroundMode="none" 在父 TextInputLayout

            【讨论】:

            • 正确答案我也由此获得
            • 它删除了基线,但添加了一个额外的顶部填充
            【解决方案8】:

            简单的方法;

            app:boxBackgroundMode="filled"
            app:boxStrokeWidth="0dp"
            

            【讨论】:

              【解决方案9】:
              <style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.NoActionBar">
                  <item name="textInputStyle">@style/Widget.MyApp.TextInputLayout</item>
              </style>
              
              <style name="Widget.MyApp.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
                  <item name="boxStrokeWidth">0dp</item>
                  <item name="boxStrokeWidthFocused">0dp</item>
              </style>
              

              【讨论】:

                【解决方案10】:

                像这样制作你的自定义 TextInputLayout

                package com.google.android.material.textfield
                
                import android.content.Context
                import android.util.AttributeSet
                class TextInputLayoutWithoutUnderline @JvmOverloads constructor(
                        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
                ) : TextInputLayout(context, attrs, defStyleAttr) {
                
                    override fun updateEditTextBackground() {
                        // XXX don't call super on purpose
                    }
                
                }
                

                【讨论】:

                  【解决方案11】:

                  res/drawable

                  处创建一个选择器
                  <?xml version="1.0" encoding="utf-8"?>
                  <selector xmlns:android="http://schemas.android.com/apk/res/android">
                      <item android:alpha="0.01" android:color="?attr/background" android:state_focused="true"/>
                      <item android:alpha="0.01" android:color="?attr/background" android:state_hovered="true"/>
                      <item android:alpha="0.01" android:color="?attr/background" android:state_enabled="false"/>
                      <item android:alpha="0.01" android:color="?attr/background"/>
                  </selector>
                  

                  背景 - 任何接近你背景的颜色。

                  方式 1 然后将其设置为您的TextInputLayout

                  <com.google.android.material.textfield.TextInputLayout
                  ...
                    app:boxStrokeColor="@drawable/text_input_selector"
                  ...
                  

                  方式2,槽型:

                  styles.xml

                  <style name="vm_textFilledBox" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
                          <item name="boxStrokeColor">@drawable/text_input_selector</item>
                  </style>
                  
                   <com.google.android.material.textfield.TextInputLayout
                  ...
                      style="@style/vm_textFilledBox"
                  

                  【讨论】:

                    【解决方案12】:

                    看起来材质组件库使用app:boxStrokeColor 绘制了自己的下划线。该属性是ColorStateList,因此您必须创建一个颜色状态列表资源,其中所有状态的颜色都设置为透明。所以基本上你想创建一个新文件res/color/filename.xml:

                    <?xml version="1.0" encoding="utf-8"?>
                    <selector xmlns:android="http://schemas.android.com/apk/res/android">
                        <item
                            android:color="@android:color/transparent"
                            android:state_pressed="true"
                            android:state_focused="true"
                            android:state_selected="true"
                            android:state_checkable="true"
                            android:state_checked="true"
                            android:state_enabled="true"
                            android:state_window_focused="true" />
                    </selector>
                    

                    并将app:boxStrokeColor 属性设置为@color/filename

                    但是,这给我留下了一条黑色下划线,它仍然没有响应 android:background=@null&lt;item name="colorControl*"&gt;@android:color/transparent&lt;/item&gt;


                    TL;DR

                    快速修复:如果您使用style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" 设置TextInputLayout 轮廓,则该框看起来几乎相同,但下划线消失了。为了删除笔画,只需将app:boxStrokeColor 属性设置为@null

                    【讨论】:

                      【解决方案13】:

                      将 TextInputLayout 的背景设置为可绘制对象,而将 TextInputEditText 的背景设置为透明的会删除下划线:

                      <com.google.android.material.textfield.TextInputLayout
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:layout_margin="8dp"
                              android:hint="@string/email"
                              android:background="@drawable/blue_drawable"
                              app:endIconMode="clear_text"
                              app:endIconTint="@color/black"
                              app:hintTextColor="@color/black">
                      
                          <com.google.android.material.textfield.TextInputEditText
                                  android:layout_width="match_parent"
                                  android:layout_height="wrap_content"
                                  android:textColor="@color/black"
                                  android:background="@android:color/transparent"
                                  android:textSize="18sp"
                                  android:layout_margin="8dp" />
                      
                      </com.google.android.material.textfield.TextInputLayout>
                      

                      blue_drawable.xml

                      <shape xmlns:android="http://schemas.android.com/apk/res/android"
                          android:shape="rectangle">
                      <corners android:radius="20dp"/>
                      <solid android:color="@android:color/holo_blue_bright"/></shape>
                      

                      【讨论】:

                      • android:background="@android:color/transparent" 设置为TextInputEditText 会从TextInputLayout 中移除蓝色背景,因此无法解决我的问题
                      • 我通过添加一个drawable作为TextInputLayout的背景来解决这个问题
                      【解决方案14】:

                      尝试将背景设置为@null@android:color/transparent

                      【讨论】:

                      • 问题中提到,我试过了,但没用
                      猜你喜欢
                      • 2020-01-24
                      • 2011-04-26
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-11-10
                      • 2018-08-24
                      • 2023-03-17
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多