Android Growth Diary-use ToggleButton to implement light switches

Source: Internet
Author: User

Case Demo

This case implementation idea: through the ToggleButton control, the ImageView control implements

---XML code:

<!--texton:true TEXTOFF:FALASE[S1]--

<togglebutton

android:id="@+id/toggleb utton1"

Android:layout_width="Match_parent"

android:layout_height="Wrap_content"

Android:checked="false[S2] "

android:texton=" open [S3] "

android:textoff=" off " />

< ImageView

android:id="@+id/imageview1"

Android:layout_width="Match_parent"

android:layout_height="Match_parent"

android:background="@drawable/off" />

----Java code:

1. Just as the general control of the use of ideas

(first define the control, initialize the control)

Private ToggleButton TB;

Private ImageView img;

tb= (ToggleButton) Findviewbyid (r.id. ToggleButton1);

Img= (ImageView) Findviewbyid (r.id. ImageView1);

2. Setting the control's listener

-----------------monitoring function via interface

--public class Mainactivity extends Activity implements Oncheckedchangelistener ()

--tb.setoncheckedchangelistener (this);

-- The method required to write out this interface

Public void oncheckedchanged (Compoundbutton Buttonview, boolean isChecked) {

/*

* When TB is clicked, the current method is executed

* Buttonview------represents the clicked control itself

* isChecked-----Represents the state of the clicked control

*

* When clicking on this TB, replace the IMG background

*/

Img.setbackgroundresource (isChecked? R.drawable. on: r.drawable. off [S4]);

}

[S1]texton: When the button is clicked

Textoff: When the button is not clicked

[S2] This statement can be written without writing, indicating the current state

[S3] Setting text state

[S4] Here is a conditional statement

Android Growth Diary-use ToggleButton to implement light switches

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.