Multi-select button (CheckBox)

Source: Internet
Author: User

Today we are introducing the CheckBox multi box:

1.Activity

//check box, [base control]---State toggle control Compoundbutton and its subclasses checkbox, RadioButton, ToggleButton, switch event Listener and scenario usage Public classCheckboxactivityextendsActivityImplementscompoundbutton.oncheckedchangelistener{Privatecontext Context; PrivateCheckBox Sleepcheckbox; PrivateCheckBox Dadoudoucheckbox; PrivateCheckBox Gamecheckbox; PrivateCheckBox Shoppingcheckbox; PrivateCheckBox Filmcheckbox; PrivateCheckBox Sportcheckbox; PrivateButton Submitbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.check_box);        Init ();            Addaction (); }        Private voidinit () {Context= This; Sleepcheckbox=(CheckBox) Findviewbyid (R.ID.SLEEPCHECKBOXID); Dadoudoucheckbox=(CheckBox) Findviewbyid (R.ID.DADOUDOUCHECKBOXID); Gamecheckbox=(CheckBox) Findviewbyid (R.ID.GAMECHECKBOXID); Shoppingcheckbox=(CheckBox) Findviewbyid (R.ID.SHOPPINGCHECKBOXID); Filmcheckbox=(CheckBox) Findviewbyid (R.ID.FILMCHECKBOXID); Sportcheckbox=(CheckBox) Findviewbyid (R.ID.SPORTCHECKBOXID); Submitbutton=(Button) Findviewbyid (R.id.submitbuttonid); }        Private voidAddaction () {Sleepcheckbox.setoncheckedchangelistener ( This); Dadoudoucheckbox.setoncheckedchangelistener ( This); Gamecheckbox.setoncheckedchangelistener ( This); Shoppingcheckbox.setoncheckedchangelistener ( This); Filmcheckbox.setoncheckedchangelistener ( This); Sportcheckbox.setoncheckedchangelistener ( This); Submitbutton.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {//String literal constant//stringbuffer string variable (thread safe)//StringBuilder string variable (non-thread safe)StringBuilder SB =NewStringBuilder ("Your interest is:"); //Mystringbuilder.insert (6, "Beautiful"); //Mystringbuilder.remove (5,7); //mystringbuilder.replace ('! ', '? '); //The code example specifies that the Mystringbuilder object can be expanded to a maximum of 25 blanks. //Stringbuildermystringbuilder = new StringBuilder ("Hello world!", +);                if(sleepcheckbox.ischecked ()) {Sb.append (Sleep); }                if(dadoudoucheckbox.ischecked ()) {Sb.append ("Hit the peas."); }                if(gamecheckbox.ischecked ()) {Sb.append (Game); }                if(shoppingcheckbox.ischecked ()) {Sb.append (Shopping); }                if(filmcheckbox.ischecked ()) {Sb.append (Movie); }                if(sportcheckbox.ischecked ()) {Sb.append (Movement);            } toast.maketext (Context, sb.tostring (), Toast.length_long). Show ();    }        }); } @Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) {        intID =Buttonview.getid (); Switch(ID) { CaseR.id.sleepcheckboxid:if(isChecked) {toast.maketext (context,"You chose to sleep.", Toast.length_short). Show (); }Else{toast.maketext (context,"You have deselected \" Sleep \ ", Toast.length_short). Show (); }             Break;  CaseR.id.dadoudoucheckboxid:if(isChecked) {toast.maketext (context,"You chose \" hit the Peas. ", Toast.length_short). Show (); }Else{toast.maketext (context,"You have deselected \" hit the Peas. ", Toast.length_short). Show (); }             Break;  CaseR.id.gamecheckboxid:if(isChecked) {toast.maketext (context,"You have chosen \" Game \ "", Toast.length_short). Show (); }Else{toast.maketext (context,"You have deselected \" game \ "", Toast.length_short). Show (); }             Break;  CaseR.id.shoppingcheckboxid:if(isChecked) {toast.maketext (context,"You have selected \" Shopping "", Toast.length_short). Show (); }Else{toast.maketext (context,"You have deselected \" shopping \ ", Toast.length_short). Show (); }             Break;  CaseR.id.filmcheckboxid:if(isChecked) {toast.maketext (context,"You chose the movie \" ", Toast.length_short). Show (); }Else{toast.maketext (context,"You have deselected \" movie \ "", Toast.length_short). Show (); }             Break;  CaseR.id.sportcheckboxid:if(isChecked) {toast.maketext (context,"You chose the sport.", Toast.length_short). Show (); }Else{toast.maketext (context,"You have deselected \" Sport \ "", Toast.length_short). Show (); }             Break; }    }    }

The 2.xml files are as follows:

<?xml version= "1.0" encoding= "Utf-8"?><!--check box page--><linearlayout xmlns:android= "/http" Schemas.android.com/apk/res/android "Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"android:padding= "5DP" > <TextView android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Interest:"android:textsize= "20sp"/> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <CheckBox Android:id= "@+id/sleepcheckboxid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Sleeping"/> <CheckBox Android:id= "@+id/dadoudoucheckboxid"Android:layout_width= "85DP"Android:layout_height= "Wrap_content"Android:text= "Hit the Peas"/> <CheckBox Android:id= "@+id/gamecheckboxid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Game"/> </LinearLayout> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <CheckBox Android:id= "@+id/shoppingcheckboxid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Shopping"/> <CheckBox Android:id= "@+id/filmcheckboxid"Android:layout_width= "85DP"Android:layout_height= "Wrap_content"Android:text= "movie"/> <CheckBox Android:id= "@+id/sportcheckboxid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Sport"/> </LinearLayout> <Button Android:id= "@+id/submitbuttonid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Right"Android:text= "OK"android:textsize= "20SP"/></linearlayout>

3. as follows:

Multi-select button (CheckBox)

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.