Android control--togglebutton Multi-Status button (switch to implement bulb)

Source: Internet
Author: User

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)

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.