The project's functional implementation displays a single box and a multi-box in an activity
The following code is the code in the Mainactivity.java file
PackageCom.example.checkbox;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.CheckBox;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.RadioButton;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {//declaring a Control object PrivateRadiogroup Gendergroup =NULL; PrivateRadioButton Femalebutton =NULL; PrivateRadioButton Malebutton =NULL; PrivateCheckBox Swimbox =NULL; PrivateCheckBox Runbox =NULL; PrivateCheckBox Readbox =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //to get the object that represents the control through the control's IDGendergroup =(Radiogroup) Findviewbyid (R.id.gendergroup); Femalebutton=(RadioButton) Findviewbyid (R.id.femalebutton); Malebutton=(RadioButton) Findviewbyid (R.id.malebutton); Swimbox=(CheckBox) Findviewbyid (r.id.swim); Runbox=(CheckBox) Findviewbyid (R.id.run); Readbox=(CheckBox) Findviewbyid (R.id.read); //Setting up ListenersGendergroup.setoncheckedchangelistener (NewRadiogroup.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) { //TODO auto-generated Method Stub if(Femalebutton.getid () = =Checkedid) {System.out.println ("Female"); Toast.maketext (mainactivity. This, "female", Toast.length_short). Show (); } Else if(Malebutton.getid () = =Checkedid) {System.out.println ("Male"); Toast.maketext (mainactivity. This, "Male", Toast.length_short). Show (); } } }); //to add a listener to a multi-select buttonSwimbox.setoncheckedchangelistener (NewCompoundbutton.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) { //TODO auto-generated Method Stub if(isChecked) {System.out.println ("Swim is checked"); } Else{System.out.println ("Swim is unchecked"); } } }); Runbox.setoncheckedchangelistener (NewCompoundbutton.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) { //TODO auto-generated Method Stub if(isChecked) {System.out.println ("Run is checked"); } Else{System.out.println ("Run is unchecked"); } } }); Readbox.setoncheckedchangelistener (NewCompoundbutton.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) { //TODO auto-generated Method Stub if(isChecked) {System.out.println ("Read is checked"); } Else{System.out.println ("Read is unchecked"); } } }); }}
The following code is the code in the Activity_main.xml file
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/linearlayout1"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Vertical"Tools:context= "${relativepackage}.${activityclass}" > <TextView Android:id= "@+id/textview1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world"/> <Radiogroup Android:id= "@+id/gendergroup"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "Vertical" > <RadioButton Android:id= "@+id/femalebutton"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/female"/> <RadioButton Android:id= "@+id/malebutton"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/male"/> </RadioGroup> <CheckBox Android:id= "@+id/swim"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/swim"/> <CheckBox Android:id= "@+id/run"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/run"/> <CheckBox Android:id= "@+id/read"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/read"/> </LinearLayout>
The following code is the code in the String.xml file
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >checkbox</ string> <string name= "Hello_world" >hello world!</string> <string name= "female" > Women < /string> <string name= "male" > Men </string> <string name= "Swim" > Swimming </string> <string name= "Run" > Run </string> <string name= "read" > Reading </string></resources>
Android Learning Note--checkbox