A check button can select several options, unlike a radio button, where the icon for the check button is a block and the radio button is a circle
A checkbox indicates that the checkbox is a subclass of button and supports all properties of the button
One, because the check box can select multiple items, all in order to determine whether the user selected an item, you also need to add a Setoncheckedchangelistener event listener for each option
For example:
Add a status Change event listener for the check button with ID like1, as follows
1Final CheckBox like1 =(CheckBox) Findviewbyid (r.id.like1);2 //Monitoring Events3 4Like1.setoncheckedchangelistener (NewOncheckedchangelistener ()) {5 6 @Override7 Public voidoncheckedchanged (Compoundbutton arg0, Boolean arg1) {8 //TODO auto-generated Method Stub9 if(like1.ischecked ())Ten Like1.gettext (); One } A});
Ii. Examples of Use
Look at the layout file first
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Choose your Hobby"android:textsize="19DP"/> <CheckBox android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:id="@+id/id_checkbox_1"Android:text="Music"/> <CheckBox android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:id="@+id/id_checkbox_2"Android:text="Art"/> <CheckBox android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:id="@+id/id_checkbox_3"Android:text="Sports"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Submit"Android:id="@+id/btn_checkbox_tijiao"/></linearlayout>
:
Look at the Java file again
1 Package base_ui;2 3 import COM.EXAMPLE.ALLCODE.R;4 5 import android.app.Activity;6 import Android.os.Bundle;7 import Android.view.View;8 import Android.view.View.OnClickListener;9 import Android.widget.Button;Ten import Android.widget.CheckBox; One import android.widget.Checkable; A import Android.widget.CompoundButton; - import Android.widget.RadioGroup.OnCheckedChangeListener; - import Android.widget.Toast; the - Public classUi_checkbox extends Activity implements android.widget.compoundbutton.oncheckedchangelistener{ - PrivateButton Tijiao; - PrivateCheckBox checkbox_1; + PrivateCheckBox checkbox_2; - PrivateCheckBox checkbox_3; + PrivateOncheckedchangelistener Checkbox_listen; A @Override at protected voidonCreate (Bundle savedinstancestate) { - //TODO auto-generated Method Stub - super.oncreate (savedinstancestate); - Setcontentview (r.layout.base_ui_checkbox); - -Tijiao =(Button) Findviewbyid (R.id.btn_checkbox_tijiao); in -Checkbox_1 =(CheckBox) Findviewbyid (r.id.id_checkbox_1); toCheckbox_2 =(CheckBox) Findviewbyid (r.id.id_checkbox_2); +Checkbox_3 =(CheckBox) Findviewbyid (r.id.id_checkbox_3); -Tijiao =(Button) Findviewbyid (R.id.btn_checkbox_tijiao); the *Checkbox_1.setoncheckedchangelistener ( This); $Checkbox_2.setoncheckedchangelistener ( This);Panax NotoginsengCheckbox_3.setoncheckedchangelistener ( This); - theTijiao.setonclicklistener (NewOnclicklistener () { + A @Override the Public voidOnClick (View v) { + //TODO auto-generated Method Stub -String str="";//the value that holds the selected option $ if(checkbox_1.ischecked ()) $Str+=checkbox_1.gettext (). toString () +" "; - if(checkbox_2.ischecked ()) -Str+=checkbox_2.gettext (). toString () +" "; the if(checkbox_3.ischecked ()) -Str+=checkbox_3.gettext (). toString () +" ";WuyiToast.maketext (Ui_checkbox. This,"your preferred hobby is:"+STR,1). Show (); the - Wu } - }); About } $ //Monitoring Events - @Override - Public voidoncheckedchanged (Compoundbutton arg0, Boolean arg1) { - //TODO auto-generated Method Stub A + } the -}
As you can see, the code is very simple and there is only one way to learn
Checkbox_1.ischecked ()
Returns whether the Checkbox_1 corresponding Check button control is selected
:
Simple use of the Android development _ Check Button control (checkbox)