如何将选择器添加到View的边框

时间:2013-07-22 10:11:25

标签: android selector

我有一个带有黑色边框的edittextview。如何使用选择器根据状态更改此边框颜色。

我试过了:

<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>

修改

我将 selector_black_border.xml 更改为颜色文件夹

<?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" />

但是即使没有聚焦/按下它也总是保持蓝色(“#FF269ff5”)。

谢谢。

1 个答案:

答案 0 :(得分:2)

您必须将选择器放在color/

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