Changing the color of EditText's underline

2019-04-26 07:30发布

问题:

I have tried to change the underline color of my EditText with the help of this thread.

I did the same thing - this is the EditText with the android:background set as @drawable/edt_bg_selector.

<EditText
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:ems="10"
         android:id="@+id/editText4"
         android:layout_weight="1"
         android:elevation="0dp"
         android:background="@drawable/edt_bg_selector" />

My edt_bg_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/edt_bg_selected" />
    <item android:state_focused="false" android:drawable="@drawable/edt_bg_normal" />
</selector>

... edt_bg_normal.xml ...

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1px"
                android:color="@color/colorWhite" />

            <solid android:color="#00FFFFFF" />

            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />
        </shape>
    </item>
</layer-list>

... and the last, edt_bg_selected.xml .

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1px"
                android:color="@color/colorWhite" />

            <solid android:color="#00FFFFFF" />

            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />
        </shape>
    </item>
</layer-list>

With this code I can see the color of underline has changed to white in Design Editor of Android Studio. But when I run this project on android device, the changes haven't been made. Selected EditText's underline is my primaryColor and unselected EditText is black.

Android studio design editor:

Project run on android device:

Does this have anything to do with the selected theme of my application? Or what did I miss?

回答1:

<EditText
    android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Something or Other"
            android:backgroundTint="@android:color/holo_green_light" />


回答2:

Add these attributes to your activity theme:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorControlNormal">@android:color/white</item>
    <item name="colorControlActivated">@android:color/white</item>
    <item name="colorControlHighlight">@android:color/white</item>
</style>


回答3:

Programmatically just implement this line:

inputText.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.error), PorterDuff.Mode.SRC_ATOP);

inputText is your EditText object.



回答4:

Ok, I have no idea what happened but the code, I posted above, works. I haven't changed anything, but I restarted the Android studio.

Before I posted this, I did the project rebuild under Build > Rebuild Project and it didn't help.

If somebody can explain why this happened or what should I do in this case, I would be grateful, but the problem is solved.