How to underline an EditText

2019-06-24 20:33发布

问题:

I am having some problems to underline an EditText in AndroidStudio.

This is what I am looking for (this is just a photo, not my real text):

enter image description here

But I do not really know any property to do that.

My code right now is really simple. Just the "normal" one:

 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_margin="16dp"
        android:id="@+id/tx_titulo"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="@string/pt_titulo"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:allowUndo="true"
        />

I do not want to underline just the text (in this case "Título"), but the whole PlainText

Does anybody know it? Greets.

回答1:

It is default, but try to check your styles. Maybe there is a style that will override the style in the whole application. If there is, just remove it



回答2:

To do this programatically, you could find an answer in this post.

To set the underline color:

editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);

To remove the underline color:

editText.getBackground().clearColorFilter();

To do it from the XML, you could add background property to do the same thing in your layout.xml with any drawable resource as a background by adding:

android:background="@drawable/mydrawable"

If you have a couple of EditText with the same style configuration, to avoid setting the attribute to each EditText, you could override EditText's default attributes, defining a custom style like it is done here.