Activity_main.xml
<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 "Tools:context = "Com.vincentlin.checkbox.MainActivity" > <checkbox android:id= "@+id/checkbox1" Android:layout_widt H= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/basketball"/></linearlayo Ut>
Strings.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "App_name" >CheckBox</string>
<string name= "Hello_world" >hello world!</string>
<string name= "Action_settings" >Settings</string>
<string name= "basketball" > Basketball </string>
</resources>
Mainactivity.java
Package com.vincentlin.checkbox;import android.app.activity;import android.os.bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.checkbox;import android.widget.compoundbutton;import android.widget.compoundbutton.oncheckedchangelistener;public class mainactivity extends activity { private checkbox checkbox; @Override protected void OnCreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); //Initialize checkbox checkbox = (checkbox) findviewbyid (r.id.checkbox1) //the checkbox is selected by setting the Listener event for the checkbox Checkbox.setoncheckedchangelistener (New oncheckedchangelistener () { @Override public void oncheckedchanged (compoundbutton buttonview, boolean ischecked) { //to listen for the current checkbox is selected by oncheckedchanged &NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("tag", ischecked+ ""); if (isChecked) { //get the text content of a checkbox String text = Checkbox.gettext (). toString (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("tag", text); } } }); }}
Android Basic three checkbox