0

I have an edittextview with a black colored border. How can I change this border color depending on the state, using a selector.

I tried this:

<EditText
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:background="@drawable/black_rounded_borders"
/>

black_rounded_borders.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#FFFFFFFF" />

    <stroke
        android:width="2dp"
        android:color="@drawable/selector_black_border" />


    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

</shape>

selector_black_border.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="#FF269ff5"/>
    <item android:state_focused="true" android:color="#FF269ff5"/>
    <item android:color="#FF000000"/>

</selector>

EDIT:

I changed selector_black_border.xml to color folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true" android:color="#FF269ff5"/>
    <item android:state_pressed="true" android:color="#FF269ff5"/>
    <item android:state_selected="true" android:color="#FF269ff5"/>
    <item android:state_focused="false" android:color="#FF000000"/>
    <item android:state_pressed="false" android:color="#FF000000"/>
    <item android:state_selected="false" android:color="#FF000000"/>

</selector>


 <stroke
       android:width="2dp"
       android:color="@color/selector_black_border" />

But then it always remain in blue color("#FF269ff5") even when not focused/pressed.

Thank You.

4

1 回答 1

3

you have to put the selector inside color/ and

<stroke
        android:width="2dp"
        android:color="@color/nameofcolor">
于 2013-07-22T10:16:55.817 回答