How to hide underbar in EditText

2020-01-26 12:41发布

How can I hide the EditText underbar (the prompt line with little serifs at the ends)?

There might be a better way to do what I want: I have a layout with an EditText. Normally, this displays fine where the user can tap on it and begin entering or editing text.

Sometimes, however, I would like to use the same layout (simplifies other logic) to display the same data in a read-only manner. I want the presentation to be similar - it should have the same height and same font, but not have the underbar.

As a stop-gap measure, I'm going to implement this by removing the EditText and substituting a TextView. I think that will give the desired results, but it seems like a roundabout an expensive way to do something that ought to be easy to do by changing attributes.

23条回答
手持菜刀,她持情操
2楼-- · 2020-01-26 12:52

You can set the EditText to have a custom transparent drawable or just use

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

or

android:background="@null"
查看更多
相关推荐>>
3楼-- · 2020-01-26 12:54

Set background to null.

android:background="@null"
查看更多
4楼-- · 2020-01-26 12:54

You can do it programmatically using setBackgroundResource:

editText.setBackgroundResource(android.R.color.transparent);
查看更多
地球回转人心会变
5楼-- · 2020-01-26 12:54

Using either property:

android:background="@null"

OR

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

worked for me to hide the underline of the EditText.

However, do note that it then causes a spacing issue with the TextInputLayout that I've surrounding the EditText

查看更多
三岁会撩人
6楼-- · 2020-01-26 12:56

You can set EditText's backgroundTint value to a specific color. If you set transparent color, underbar should gone.

android:backgroundTint="@color/Transparent"

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

But you can use this in Api v21(Lollipop) or higher

查看更多
小情绪 Triste *
7楼-- · 2020-01-26 12:58

if your edit text already has a background then you can use following.

android:textCursorDrawable="@null"
查看更多
登录 后发表回答