First, there are two kinds of states:
Checked state (true), unchecked (false)
Second, the attribute
Android:id = "@+id/checkbox"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:checked = "false"
Android:text = "Male"
Three, the code demonstration
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Horizontal" > <CheckBoxAndroid:id= "@+id/checkbox1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Basketball"android:checked= "true" /> </LinearLayout>
PackageCom.muke.textview_edittext;ImportAndroid.os.Bundle;ImportAndroid.widget.CheckBox;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.CompoundButton.OnCheckedChangeListener;ImportAndroid.widget.ImageView;ImportAndroid.widget.ToggleButton;Importandroid.app.Activity; Public classMainactivityextendsactivity{Privatecheckbox checkbox; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //First step: Initialize the control (find the control you want to manipulate)CheckBox =(CheckBox) Findviewbyid (r.id.checkbox1); //Step Two: Check if checkbox is selected by setting the checkbox's Listener eventCheckbox.setoncheckedchangelistener (NewOncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) { //Step Three: monitor whether the current checkbox is selected by OnCheckedChangedString text = Checkbox.gettext (). toString ();//get the text content of a checkbox if(isChecked) {System.out.println ("You have selected" +text); }Else{System.out.println ("You canceled" +text); } } }); } }
CheckBox for Android Control (check box control)