How to change the color of button after click?

别说谁变了你拦得住时间么 提交于 2019-12-01 01:58:51

问题


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 button is
Click.
I don't understand how to do this?
Give me suggestion.
here is my button code.

<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="@color/myMaroonColor" 
      android:layout_width="54dp" 
      android:layout_height="wrap_content" ></Button>

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/9685261/how-to-change-the-color-of-button-after-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!