Android Development CheckBox Control, select All, deselect, cancel Select all

Source: Internet
Author: User

In Android development we often use the CheckBox control, then how to implement the CheckBox control of the full selection, inverse selection?

First layout Our interface:

<?xml version= "1.0" encoding= "Utf-8"?><RelativelayoutXmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Match_parent"android:layout_height="Match_parent"xmlns:app="Http://schemas.android.com/apk/res-auto" ><TextViewAndroid:id="@+id/textviewtitle"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_margintop="30DP"android:text="What do you usually like to do?" "/><CheckBoxAndroid:id="@+id/checkboxall"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:layout_alignbottom="@id/textviewtitle"android:layout_aligntop="@id/textviewtitle"android:layout_torightof="@id/textviewtitle"android:text="Select All"/>Checkbox--> of <!--content<CheckBoxAndroid:id="@+id/checkbox1"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_below="@id/textviewtitle"android:layout_marginright="80DP"android:text="Play the Game"/><CheckBoxAndroid:id="@+id/checkbox2"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_below="@id/textviewtitle"android:layout_torightof="@+id/checkbox1"android:text="Learning"/><CheckBoxAndroid:id="@+id/checkbox3"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_below="@id/checkbox1"android:text="Knock Code"/><CheckBoxAndroid:id="@+id/checkbox4"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_below="@id/checkbox2"android:layout_torightof="@+id/checkbox1"android:text="Running"/><CheckBoxAndroid:id="@+id/checkbox5"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_below="@id/checkbox3"android:text="Swimming"/><CheckBoxAndroid:id="@+id/checkbox6"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_below="@id/checkbox4"android:layout_torightof= "@+id/checkbox1" android:text="Sleep"/> <TextView Android : id="@+id/textviewinfo" android:layout_width="match_parent" android:layout_height= "wrap_ Content " android:layout_below=" @id/checkbox5 " android:layout_margintop=" 20DP " android: Text="selected:"/></relativelayout>         

Note To write an ID to the corresponding checkbox,

Next is the Java code: the main implementation of the Code function, click the Select All button, I will
Select all the options, and if I deselect any of the checkboxes, then the Select All button will also be deselected, and the Select all check box will be selected.

The code is as follows:

Package COM.DC.WORK3;Import Android.os.Bundle;Import android.support.v7.app.AppCompatActivity;Import Android.util.Log;Import Android.widget.CheckBox;Import Android.widget.CompoundButton;Import Android.widget.TextView;Import Android.widget.Toast;Import java.util.LinkedList;Import java.util.List;/** * Created by Dinciensen */PublicClassMainactivity2sExtendsappcompatactivity {Private CheckBox Checkboxall;Private CheckBox CheckBox1;Private CheckBox CheckBox2;Private CheckBox CheckBox3;Private CheckBox checkBox4;Private CheckBox checkBox5;Private CheckBox checkBox6;Private TextView Textviewinfo;Private list<string> checkedstr;When the operation cancels one, the Select all cancels, this variable is whether the user clicksPrivateBoolean checkfouser=TrueProtectedvoidOnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main_2); Checkboxall = (CheckBox) Findviewbyid (R.id.checkboxall); CheckBox1 = (CheckBox) Findviewbyid (r.id.checkbox1); CheckBox2 = (CheckBox) Findviewbyid (R.ID.CHECKBOX2); CheckBox3 = (CheckBox) Findviewbyid (R.ID.CHECKBOX3); CheckBox4 = (CheckBox) Findviewbyid (R.ID.CHECKBOX4); CHECKBOX5 = (CheckBox) Findviewbyid (R.ID.CHECKBOX5); checkBox6 = (CheckBox) Findviewbyid (r.id.checkbox6); Textviewinfo = (TextView) Findviewbyid (r.id.textviewinfo); Checkbox1.setoncheckedchangelistener (ChangeListener); Checkbox2.setoncheckedchangelistener (ChangeListener); Checkbox3.setoncheckedchangelistener (ChangeListener); Checkbox4.setoncheckedchangelistener (ChangeListener); Checkbox5.setoncheckedchangelistener (ChangeListener); Checkbox6.setoncheckedchangelistener (ChangeListener); Checkboxall.setoncheckedchangelistener (ChangeListener); Checkedstr=New Linkedlist<> (); }Public Compoundbutton.oncheckedchangelistener ChangeListener =New Compoundbutton.oncheckedchangelistener () {@OverridePublicvoidOnCheckedChanged (Compoundbutton Buttonview,Boolean isChecked) {Switch (Buttonview.getid ()) {Case R.ID.CHECKBOX1:Case R.ID.CHECKBOX2:Case R.ID.CHECKBOX3:Case R.ID.CHECKBOX4:Case R.ID.CHECKBOX5:Case R.id.checkbox6:string Str=buttonview.gettext (). toString ();if (isChecked) {checkedstr.add (str);}else {checkedstr.remove (str);}Checkfouser=false; Checkboxall.setoncheckedchangelistener (NULL);if (checkbox1.ischecked () &&checkbox2.ischecked () &&checkbox3.ischecked () && Checkbox4.ischecked () &&checkbox5.ischecked () &&checkbox6.ischecked ()) {Indicates that if all is selected, the Select All button is also selected checkboxall.setchecked (true); }else {Otherwise the Select All button to uncheck, but this will trigger the Checkboxall listening, will be all canceled checkboxall.setchecked (FALSE); }Checkfouser=true;/* The second method */Checkboxall.setoncheckedchangelistener (ChangeListener); Break ; Case R.id.checkboxall: if (checkfouser) {checkbox1.setchecked (isChecked); checkbox2.setchecked (isChecked); Checkbox3.setchecked (isChecked); Checkbox4.setchecked (isChecked); Checkbox5.setchecked (isChecked); Checkbox6.setchecked (isChecked); Break ;} } stringbuffer sb=new StringBuffer (); for (String str:checkedstr) {sb.append (str+",");} if (sb.length () >0) { //Set length to 1, remove last "," Sb.setlength (Sb.length ()-1);} textviewinfo.settext ( "Selected:" +sb.tostring ()); } };}

Writing is not very good, but can be simple to solve the problem, welcome everyone to actively communicate, leave valuable suggestions, learn together!

Android Development CheckBox Control, select All, deselect, cancel Select all

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.