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