Change border color when edittext is focused

2019-01-11 21:05发布

问题:

I am wondering how I can change the border color of the edittext when it is focused, at the moment it looks like this:

I have tried checking in the SDK for the source picture, but I couldn't make any sense of it, and I have also tried with xml, but was unable to change only the border color.

If I find the source picture I could edit in photoshop to change the color, any tips on how I could do this, or where the source picture is located? :)

回答1:

You can create a custom shape for your EditText

1: STATE_ENABLED is to check if the ET is enabled/disabled.

2: STATE_PRESSED is to check when the ET is pressed (it will change the color on the press but won't keep it that way.)

3: STATE_FOCUSED keeps the ET border color changed as long as the ET is focused(This fulfills the requirement).

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true"
            android:state_focused="true">
        <shape android:padding="10dp" android:shape="rectangle">
            <solid android:color="#FFFFFF" />
            <stroke android:width="2dp" android:color="#ffa0a496" />
            <corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp" />
        </shape>
    </item>
    <item android:state_enabled="true">
        <shape android:padding="10dp"
            android:shape="rectangle">
            <solid android:color="#FFFFFF" />
            <corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp" />
        </shape>
    </item>
</selector>

And set background:

editText.setBackgroundResource(R.drawable.yourFile);