1. First create a button
<button
Android:id= "@+id/click"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Click to Color"
android:background= "@drawable/btn_st"
android:gravity= "Center"/>
The background of the button created in 2, 1 is used for Btn_st, created under Drawable btn_st
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_activated= "true" android:drawable= "@drawable/color1" ></item> <!--color when not clicked--
<item android:state_activated= "false" android:drawable= "@drawable/color2" ></item> <!--clicked Color--
</selector>
3. Color1 and Color2 used in 2, added in the strings of values
<drawable name= "Color1" > #339966 </drawable>
<drawable name= "Color2" > #fff </drawable>
4. Java code
(1) First get button
Button clickbtn = (button) Findviewbyid (R.id.click);
(2) Setting a flag flag
int flag = 0;
(3) Add a listener event to the button
Clickbtn.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View v) {
Switch (flag) {
Case 0:
Mbtn[i].setactivated (FALSE);
flag = 1;
Break
Case 1:
Mbtn[i].setactivated (TRUE);
Flag = 0;
Break
}
}
}
Idea: Set the button to activate/deactivate two different colors. When the button is clicked once, three times equal odd several times, the Set button is inactive (pressed), when the button is clicked an even number of times is set to the active state (not pressed).
Android Implementation button Click Effect (first click Color, second recovery)