How to change the color of button after click?

前端 未结 2 1081
再見小時候
再見小時候 2021-01-12 22:59

I create button with background color but when i click on it, it\'s not show anything.
I need to show different color on button after click because user need to know but

相关标签:
2条回答
  • 2021-01-12 23:12
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@color/colorPrimaryDark" />
        <item android:state_focused="true" android:drawable="@android:color/holo_green_dark" />
        <item android:drawable="@color/colorCartButton" />
    </selector>
    

    This will work

    0 讨论(0)
  • 2021-01-12 23:13

    //XML file saved at res/drawable/button_bg.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="#ffff0000"/> <!-- pressed -->
        <item android:state_focused="true"
              android:color="#ff0000ff"/> <!-- focused -->
        <item android:color="#ff000000"/> <!-- default -->
    </selector>
    

    //This layout XML will apply the color list to a View:

    <Button android:textSize="15px"
          android:id="@+id/button9" 
          android:gravity="center|bottom" 
          android:textColor="@color/myWhiteColor" 
          android:drawableTop="@drawable/math"
          android:text="@string/HomePage_Math" 
          android:background="@drawable/button_bg" 
          android:layout_width="54dp" 
          android:layout_height="wrap_content" ></Button>
    
    0 讨论(0)
提交回复
热议问题