The button click status of Android natively changes, but if you add a .png format image as the background color, the button click will not have any effect. In order to achieve the effect of clicking the button, we need to prepare two pictures to switch, and the text will also change color. If you don’t say much, just click on the code:
The button background image is placed in drawable/background_button.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:andro> <item android:drawable="@drawable/bg_press" android:state_pressed="true"/> <item android:drawable="@drawable/bg_normal" android:state_enabled="true"/> <item android:drawable="@drawable/bg_normal"/> </selector>
Prepare two pictures one is bg_press.png and the other is bg_normal.png.
Set in the buttons that need to be changed:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="50dp" android:focusable="false" android:gravity="center" android:textSize="24px" android:text="@string/str_tethering_modify" android:background="@drawable/background_button" />
This will be solved if the background color changes are changed. The text on the button is below. Now the text on the button is not changed. In order to achieve the change in the button text color, we will create a new xml file.
Button color change drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:andro> <item android:state_pressed="true" android:color="#975508"/> <item android:state_focused="false" android:state_pressed="false" android:color="#E5960E"/> <item android:state_focused="true" android:color="#975508"/> <item android:state_focused="false" android:color="#E5960E"/> </selector>
Add to our button textColor
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="50dp" android:focusable="false" android:gravity="center" android:textSize="24px" android:textColor="@drawable/button_color" android:text="@string/str_tethering_modify" android:background="@drawable/background_button" />
In this way, the background and text will have a click effect. However, if there is such a requirement, the color of the button text needs to be set under certain conditions. After setting this, I found that the text clicking on our button has not changed. I tried to directly (.button_color); I found that this is useless. In this way, you need to use ColorStateList to solve it. As the name suggests, it is to define the state list of colors, and set different colors by listening to different states of the button.
The old rules, I won’t say much nonsense, just post the code:
/** * Button click color change */ private ColorStateList colorStateList; colorStateList = (ColorStateList)getResources().getColorStateList(.button_color); if(xxx){ (); }else{ (colorStateList); }
This perfectly solves the change in button click status.
Supplementary knowledge:Android Studio Setting buttons and background blend together, that is, buttons remove shadows
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onClick="sendMessage" style="?android:attr/borderlessButtonStyle" />
The above article on the Android Button button click background and text changes is all the content I share with you. I hope you can give you a reference and I hope you can support me more.