<linearlayout xmlns: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: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 "android:orientation= "Vertical" > <CheckBox Android:id= "@+id/eatid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Eat"/> <CheckBox Android:id= "@+id/sleepid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Sleeping"/> <CheckBox Android:id= "@+id/dotaid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "DotA"/> <CheckBox Android:id= "@+id/allid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Select All"/></linearlayout>
PackageCom.njulya.checkbox;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.widget.CheckBox;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.CompoundButton.OnCheckedChangeListener; Public classMainactivityextendsActivity {PrivateCheckBox Eatbox; PrivateCheckBox Sleepbox; PrivateCheckBox Dotabox; PrivateCheckBox Allbox; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Eatbox=(CheckBox) Findviewbyid (R.id.eatid); Sleepbox=(CheckBox) Findviewbyid (r.id.sleepid); Dotabox=(CheckBox) Findviewbyid (R.id.dotaid); Allbox=(CheckBox) Findviewbyid (r.id.allid); Allboxlistener Alllistener=NewAllboxlistener (); Checkboxlistener Boxlistener=NewCheckboxlistener (); Eatbox.setoncheckedchangelistener (Boxlistener); Sleepbox.setoncheckedchangelistener (Boxlistener); Dotabox.setoncheckedchangelistener (Boxlistener); Allbox.setoncheckedchangelistener (Alllistener); } Private classAllboxlistenerImplementsoncheckedchangelistener{@Override Public voidOnCheckedChanged (Compoundbutton arg0,BooleanisChecked) { if(isChecked) {eatbox.setchecked (true); Sleepbox.setchecked (true); Dotabox.setchecked (true); }Else{eatbox.setchecked (false); Sleepbox.setchecked (false); Dotabox.setchecked (false); } } } Private classCheckboxlistenerImplementsoncheckedchangelistener{@Override Public voidOnCheckedChanged (Compoundbutton checkbox,BooleanisChecked) { if(Checkbox.getid () = =R.id.eatid) {System.out.println ("Eat"); } Else if(Checkbox.getid () = =r.id.sleepid) {System.out.println ("Sleep"); } Else if(Checkbox.getid () = =R.id.dotaid) {System.out.println ("DotA"); } if(isChecked) {System.out.println ("Checked"); } Else{System.out.println ("Unchecked"); } }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
Familiar with ANDROIDAPI series 2--checkbox and Oncheckedchangelistener