Activity_main.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "com.vincentlin.togglebutton.MainActivity" > <!-- texton:true textoff:false --> <togglebutton android:id= "@+id/togglebutton1" Android:checked= "false" android:texton= "@string/on" android:textoff= "@string/off" android:layout_width= "Match_parent" android:layout_height = "Wrap_content" /> <imageview android:id= "@+id/imageview1" android:layout_ Width= "Match_parent" android:layout_height= "Match_parent" android:background= "@drawable/off" /></LinearLayout>
Strings.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >togglebutton</ string> <string name= "Hello_world" >hello world!</string> <string name= "Action_settings" >Settin Gs</string> <string name= "on" > Open </string> <string name= "Off" > Off </string></resources >
Mainactivity.java
Package com.vincentlin.togglebutton;import android.app.activity;import android.os.bundle;import android.view.menu;import android.view.menuitem;import android.widget.compoundbutton;import android.widget.compoundbutton.oncheckedchangelistener;import android.widget.imagebutton;import android.widget.imageview;import android.widget.togglebutton;public class mainactivity extends Activity implements OnCheckedChangeListener{ private ToggleButton tbutton; private imageview iview; @Override protected void oncreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); setContentView ( R.layout.activity_main); //initialization Component tButton = (ToggleButton) findviewbyid ( R.id.togglebutton1); iview = (ImageView) findviewbyid (r.id.imageview1); / * * to the currentTButton Set Listener */ tbutton.setoncheckedchangelistener (this); } @Override public void oncheckedchanged (compoundbutton buttonview, boolean isChecked) { /* * * when TButton is clicked, the current method executes * buttonview---represents the clicked control itself, * ischecked---represents the state of the control being clicked * When clicking on this TButton, change the img background */ iview.setbackgroundresource (isChecked? R.drawable.on:r.drawable.off); }}
//
650) this.width=650; "style=" Float:none; "title=" Image Off.png "src=" http://s3.51cto.com/wyfs02/M01/58/A2/ Wkiom1s2iplhsxq_aadbigqucte871.jpg "alt=" Wkiom1s2iplhsxq_aadbigqucte871.jpg "/>
650) this.width=650; "style=" Float:none; "title=" Image On.png "src=" http://s3.51cto.com/wyfs02/M02/58/9F/ Wkiol1s2icchb0wlaadyssar6v4214.jpg "alt=" Wkiol1s2icchb0wlaadyssar6v4214.jpg "/>
Android Basics Two ToggleButton