1: Add two light bulb pictures in drawable, named Off/on
2:xml file:
<togglebutton
Android:id= "@+id/tbtn"
android:texton= "Open"
Android:textoff= "Off"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<imageview
Android:id= "@+id/img"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/off"/>
3:java file:
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Initialize the control:
Setcontentview (R.layout.activity_main);
Tbtn= (ToggleButton) Findviewbyid (R.ID.TBTN);
Img= (ImageView) Findviewbyid (r.id.img);
To set up listeners:
Tbtn.setoncheckedchangelistener (this);
public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {
TODO auto-generated Method Stub
When btnt clicks, the current method executes, Buttonview: represents the clicked control itself, ischecked represents the state of the click Control, and when the button is clicked, the IMG background is replaced
Img.setbackgroundresource (isChecked? R.drawable.on,r.drawable.off);//If clicked, the picture is changed, and the trinocular operator
}
Use ToggleButton for picture replacement