how to change background image of button when clicked/focused?

后端 未结 8 1047
旧时难觅i
旧时难觅i 2020-11-30 01:00

I want to change the background image of a button when clicked or focused.

This is my code:

Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOn         


        
相关标签:
8条回答
  • 2020-11-30 01:50

    use this code create xml file in drawable folder name:button

    <?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/buutton_pressed" />
      <item 
         android:drawable="@drawable/button_image" />
    </selector>
    

    and in button xml file

     android:background="@drawable/button"
    
    0 讨论(0)
  • 2020-11-30 01:54

    You just need to set background and give previous.xml file in background of button in your layout file.

    <Button
     android:id="@+id/button1"
     android:background="@drawable/previous"
     android:layout_width="200dp"
     android:layout_height="126dp"
     android:text="Hello" />
    

    and done.Edit Following is previous.xml file in drawable directory

    <?xml version="1.0" encoding="utf-8"?>
    

    <item android:drawable="@drawable/onclick" android:state_selected="true"></item>
    <item android:drawable="@drawable/onclick" android:state_pressed="true"></item>
    <item android:drawable="@drawable/normal"></item>
    

    0 讨论(0)
提交回复
热议问题