开发者

changing image of imagebutton till it is pressed

开发者 https://www.devze.com 2023-04-05 20:59 出处:网络
I want that in my android application, the ImageButton change its image when it is pressed and released, and when it is 开发者_C百科pressed released again, the image for ImageButton will be changed ba

I want that in my android application, the ImageButton change its image when it is pressed and released, and when it is 开发者_C百科pressed released again, the image for ImageButton will be changed back , how to do that? I have tried it with selector. But it is not working. Please anyone help me. Thanks


make file in drawable folder a.xml ::

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

and in your main file :: button have implment following code ::

<ImageButton android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="anything"
        android:id="@+id/first"
        android:background="@drawable/a"
        android:textColor="#FFFFFF" 
        android:textStyle="bold"
        ></ImageButton>


use the following selector.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号