Idea: Set the path of the picture by clicking on the button's selection status and false status
1. layout file:
<LinearLayoutxmlns: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"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " > <!--Texton is true state, Textoff is false - <ToggleButtonAndroid:id= "@+id/togglebutton1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "ToggleButton"Android:textoff= "Off"Android:texton= "Open" /> <ImageViewAndroid:id= "@+id/imageview1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@drawable/img1" /></LinearLayout>
2.mainactivity.java
PackageCom.example.mooc;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.AutoCompleteTextView;ImportAndroid.widget.Button;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.CompoundButton.OnCheckedChangeListener;ImportAndroid.widget.ImageButton;ImportAndroid.widget.ImageView;ImportAndroid.widget.MultiAutoCompleteTextView;ImportAndroid.widget.ToggleButton; Public classMainactivityextendsActivityImplementsOncheckedchangelistener {PrivateButton Login_button; PrivateToggleButton ToggleButton; PrivateImageView Imgview; PrivateImageButton Img_button; PrivateAutocompletetextview AutoCompleteTextView1; PrivateMultiautocompletetextview mutiple; PrivateString res[] = {"Beijing1", "Beijing2", "Beijing3" }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //introduce the layout file to the activitySetcontentview (R.layout.toggle_button); ToggleButton=(ToggleButton) Findviewbyid (R.id.togglebutton1); Imgview=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Togglebutton.setoncheckedchangelistener ( This); } /*** ARG0: Represents the component itself * Arg1 represents the selected state*/@Override Public voidOnCheckedChanged (Compoundbutton arg0,Booleanarg1) {//This function executes when the button is clickedImgview.setbackgroundresource (arg1?R.DRAWABLE.IC_LAUNCHER:R.DRAWABLE.IMG1); }}
3. Effect: (False when display storage bag, true when display Android icon )
After clicking
Android control--togglebutton Multi-Status button (switch to implement bulb)