Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

android:state_pressed is not working , pressing the button it is not not changing the color

I have the following XML code in res/drawable and I set button background to this drawable. However when I pressed the button it is not not changing the color. Thanks for help

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/pink"/> <!-- pressed state -->
<item android:state_selected="false" android:drawable="@color/blue"/> <!-- default state -->

button :

        <Button
        android:id="@+id/button12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_weight="1"
        android:background="@drawable/button_bg"
        android:text="REGISTER"
        android:textColor="@color/white"
        android:textSize="20sp" />

color XML :

<color name="blue">#49B8C7</color>
<color name="pink">#FF8EB9</color>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your issue may caused by android:drawable="@color/pink". Seems to not working in color drawable. In this case, Please like this.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"> <!-- pressed state -->
        <shape android:shape="rectangle"><solid android:color="@color/pink"/></shape>
    </item>
    <item> <!-- default state -->
        <shape android:shape="rectangle"><solid android:color="@color/blue"/></shape>
    </item>
</selector>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...