如何去掉EditText指示器下方的下划线?

84

最近我需要更改EditText的指示器颜色,在这样做之后,一个奇怪的线条开始出现在指示器下方。我该如何去掉它?下面是我所做的代码。

enter image description here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="#4FB6E1">

    <br.com.edsilfer.kiwi.loading.CircularProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        app:colorLine="#4e2972"/>

    <pl.droidsonroids.gif.GifTextView
        android:id="@+id/flying_charizard"
        android:layout_width="100dip"
        android:layout_height="70dip"
        android:layout_above="@+id/login_cluster"
        android:layout_margin="15dip"
        android:background="@drawable/flying_charizard"/>

    <android.support.v7.widget.CardView
        android:id="@+id/login_cluster"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:elevation="4dip"
        card_view:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="10dip"
            android:paddingLeft="10dip"
            android:paddingRight="10dip">

            <include layout="@layout/rsc_util_remove_act_edittext_focus"/>

            <EditText
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="E-mail"
                android:imeOptions="actionNext"
                android:inputType="text"
                android:textColor="@color/textSecondary"
                android:textColorHint="@color/textSecondary"
                android:theme="@style/CustomEditText"/>

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:hint="Password"
                android:imeOptions="actionGo"
                android:inputType="textPassword"
                android:fontFamily="sans-serif"
                android:textColor="@color/textSecondary"
                android:textColorHint="@color/textSecondary"
                android:theme="@style/CustomEditText"/>


            <com.gc.materialdesign.views.ButtonRectangle
                android:id="@+id/login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="15dip"
                android:background="@color/textSecondary"
                android:text="@string/act_login_login"/>

            <com.gc.materialdesign.views.ButtonFlat
                android:id="@+id/register"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dip"
                android:background="@color/textSecondary"
                android:text="@string/act_login_create_account"/>

            <com.gc.materialdesign.views.ButtonFlat
                android:id="@+id/forgotPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@color/textSecondary"
                android:text="@string/act_login_forgot_password"/>

            <TextView
                android:id="@+id/copyright"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="10dip"
                android:layout_marginTop="5dip"
                android:alpha="0.7"
                android:text="@string/act_login_copyright"
                android:textAlignment="center"
                android:textColor="@color/textSecondary"
                android:textColorHint="@color/textSecondary"
                android:textSize="@dimen/textH4"
                android:textStyle="bold"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <pl.droidsonroids.gif.GifTextView
        android:id="@+id/man_running"
        android:layout_width="60dip"
        android:layout_height="70dip"
        android:layout_alignParentBottom="true"
        android:layout_margin="5dip"
        android:background="@drawable/man_running"
        android:elevation="1dp"/>

    <pl.droidsonroids.gif.GifTextView
        android:id="@+id/background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/login_background"/>
</RelativeLayout>

样式.xml

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="CustomEditText" parent="Widget.AppCompat.EditText">
    <item name="colorControlNormal">@color/colorPrimaryDark</item>
    <item name="colorControlActivated">@color/colorPrimaryDark</item>
    <item name="colorControlHighlight">@color/colorPrimaryDark</item>
</style>

这不是一个重复项,因为我不是试图删除下划线,而是出现在下划线上方的额外行。请仔细阅读描述。 - E. Fernandes
在TextInputLayout中使用app:boxStrokeWidth="0dp"来实现Material Design。 - Sumit Shukla
10个回答

136
让您的editText背景像这样 android:background="@null"

25
如果你已经使用了自定义背景图案,就不能使用这个解决方案。 :( - angryITguy
5
如果我们使用自定义背景,那么编辑文本下方的线条将不可见。 - Jinesh Francis
2
不,我使用自定义背景可绘制对象。但在这种情况下仍然出现了下划线,这并不起作用。 - Sideeg MoHammed
完全同意,使用自定义背景仍然是下划线。 - Pablo Martinez

68
如果您正在使用材料组件中的TextInputLayout,将背景颜色设置为其他颜色不会删除下划线,解决方案是像这样设置BoxBackgroundMode(kotlin): myTextInputLayout.boxBackgroundMode = TextInputLayout.BOX_BACKGROUND_NONE 或者在xml中: app:boxBackgroundMode="none" 我认为这一变化是为了使下划线与自定义背景颜色“配合使用”。

1
感谢你!!!!!!!!!!!!!!!!!!!TextInputLayout layout = new TextInputLayout(context); layout.setBoxStrokeColor(Color.TRANSPARENT); //<-- 不行 layout.setBackgroundColor(Color.TRANSPARENT); //<-- 不行 layout.setBackground(null);//<-- 不行 layout.setBackgroundResource(R.color.textboxcolor);//<-- 不行 - Mr Heelis
1
这是唯一正确的答案。其他的都是假的。 - Chapz
1
对我不起作用 :/ - kilian eller
1
谢谢,它也适用于材料编辑文本。 - Syyam Noor
2
哇!在这里提到的所有解决方法中,app:boxBackgroundMode="none"可以与自定义背景一起使用!太棒了。供参考,我正在使用一个由androidX包装的材料TextInputEditTextTextInputLayout - sud007
显示剩余2条评论

17
如果 android:background="@null" 没有帮助 - 尝试将其与 android:inputType="text|textNoSuggestions" 结合使用。
完整视图:
    <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@null"
      android:inputType="text|textNoSuggestions" />

15

EditTextbackground颜色设置为白色。

如果屏幕背景色和EditText背景色相同,则EditText下划线将不可见。您可以将屏幕背景颜色设置为EditText背景。

android:background="#FFFFFF" 

14

在使用 getText() 之前,应该先调用 edittext.clearComposingText(); 方法。或者在 .xml 中使用 android:inputType="text|textNoSuggestions" 属性。


8
即使使用了那个方法,线条仍然存在。 - E. Fernandes

8

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


8
我以编程的方式进行了如下操作:
editText.setBackground(null);

2
还可以通过xml完成 - android:background="@null" - Mark Lapasa
对我来说,editText.setBackgroundColor(null) 是有效的。 - undefined

1
你可以为editText定义一个样式,以便将所有属性进行分组。如果您需要多个需要具有相同行为的编辑文本,则非常强大。
将代码放在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" />

0

setColorFilter() 在 API 等级 5 及以下版本不起作用。一种方法是使用 setBackgroundColor(TRANSPARENT color) 并使用 clearColorFilter() 将其移除。


0

我遇到了与 OP 截图中显示的相同的问题。已接受答案 中描述的解决方案只是删除了您想要自定义的下划线。从 Widget.AppCompat.EditText 继承 样式 是可以的,但不是继承 主题。解决方法是从 CustomEditText 中删除继承:

<style name="CustomEditText"><!-- parent removed -->
    <item name="colorControlNormal">@color/colorPrimaryDark</item>
    <item name="colorControlActivated">@color/colorPrimaryDark</item>
    <item name="colorControlHighlight">@color/colorPrimaryDark</item>
</style>

感谢这个答案


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接