Android Implementation button Click Effect (first click Color, second recovery)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.