Android-checkbox Multiple Selection Demo

Source: Internet
Author: User

Code

 Packagecom.lxt008;ImportCOM.LXT008.R;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.CheckBox;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classActivity01extendsactivity{//used to display the topicTextView M_textview1; //"Submit Button"Button M_button1; //4 + OptionsCheckBox M_checkbox1;    CheckBox M_checkbox2;    CheckBox M_checkbox3;        CheckBox m_checkbox4; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); M_textview1=(TextView) Findviewbyid (R.ID.TEXTVIEW1); M_button1=(Button) Findviewbyid (R.id.button1); /*get each CheckBox object*/M_checkbox1=(CheckBox) Findviewbyid (r.id.checkbox1); M_checkbox2=(CheckBox) Findviewbyid (R.ID.CHECKBOX2); M_checkbox3=(CheckBox) Findviewbyid (R.ID.CHECKBOX3); M_checkbox4=(CheckBox) Findviewbyid (R.ID.CHECKBOX4); //Set event listener for each optionM_checkbox1.setoncheckedchangelistener (NewCheckbox.oncheckedchangelistener () { Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) {                if(m_checkbox1.ischecked ()) {Displaytoast ("You have chosen:" +M_checkbox1.gettext ());        }            }        }); ////////////////////M_checkbox2.setoncheckedchangelistener (NewCheckbox.oncheckedchangelistener () { Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) {                if(m_checkbox2.ischecked ()) {Displaytoast ("You have chosen:" +M_checkbox2.gettext ());        }            }        }); /////////////////M_checkbox3.setoncheckedchangelistener (NewCheckbox.oncheckedchangelistener () { Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) {                if(m_checkbox3.ischecked ()) {Displaytoast ("You have chosen:" +M_checkbox3.gettext ());        }            }        }); ////////////////M_checkbox4.setoncheckedchangelistener (NewCheckbox.oncheckedchangelistener () { Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) {                if(m_checkbox4.ischecked ()) {Displaytoast ("You have chosen:" +M_checkbox4.gettext ());        }            }        }); //Set event Listener on buttonM_button1.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {intnum = 0; if(m_checkbox1.ischecked ()) {num++; }                if(m_checkbox2.ischecked ()) {num++; }                if(m_checkbox3.ischecked ()) {num++; }                if(m_checkbox4.ischecked ()) {num++; } displaytoast ("Thank you for participating!" You chose the "+num+" item altogether! ");    }        }); }        /*Show Toast*/     Public voiddisplaytoast (String str) {toast Toast= Toast.maketext ( This, str, toast.length_short); //set the location of the toast displayToast.setgravity (gravity.top, 0, 220); //Show the Toasttoast.show (); }}

Layout file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"    ><TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello"    /><CheckBoxAndroid:id= "@+id/checkbox1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/checkbox1"></CheckBox><CheckBoxAndroid:id= "@+id/checkbox2"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/checkbox2"></CheckBox><CheckBoxAndroid:id= "@+id/checkbox3"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/checkbox3"></CheckBox><CheckBoxAndroid:id= "@+id/checkbox4"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/checkbox4"></CheckBox>    <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Submit"    >    </Button></LinearLayout>

Android-checkbox Multiple Selection Demo

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.