Android CheckBox usage and status acquisition, androidcheckbox
<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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_ver Tical_margin "android: orientation =" vertical "> <! -- Define the CheckBox control, which represents the basketball option --> <CheckBox android: id = "@ + id/CbBasketball" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "basketball"/> <! -- Define the CheckBox control, which represents the Table Tennis Option --> <CheckBox android: id = "@ + id/CbPingpangball" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Table Tennis"/> <! -- Define the CheckBox control to represent the football options --> <CheckBox android: id = "@ + id/CbFootball" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "soccer"/> <! -- Define the TextView control to display the selected result --> <TextView android: id = "@ + id/TvResult" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "@ string/str"/> </LinearLayout>
Package com. example. yanlei. yl2; import android. OS. bundle; import android. support. v7.app. appCompatActivity; // import essential package import android. app. activity; import android. OS. bundle; import android. widget. checkBox; import android. widget. compoundButton; import android. widget. textView; import android. widget. compoundButton. onCheckedChangeListener; public class MainActivity extends AppCompatActivity {private CheckBox CbBasketball; // defines the check box object private CheckBox CbPingpangball for basketball; // defines the check box object private CheckBox CbFootball for table tennis; // define the check box object private TextView TvResult for football; // define the result text tag object @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // call the onCreate method of the parent class // set the layout file of the current page to activity_main setContentView (R. layout. activity_main); findView (); // obtain the control setListener () in the page; // set the control listener} private void setListener () {// TODO Auto-generated method stub // sets the listener CbBasketball for changing the status of all checkboxes. setOnCheckedChangeListener (myCheckChangelistener); CbPingpangball. setOnCheckedChangeListener (myCheckChangelistener); CbFootball. setOnCheckedChangeListener (myCheckChangelistener);} OnCheckedChangeListener myCheckChangelistener = new OnCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stub // set the content of TextView to display the CheckBox selection result setText () ;}}; private void findView () {// TODO Auto-generated method stub // obtain the corresponding control object CbBasketball = (CheckBox) findViewById (R. id. cbBasketball); CbPingpangball = (CheckBox) findViewById (R. id. cbPingpangball); CbFootball = (CheckBox) findViewById (R. id. cbFootball); TvResult = (TextView) findViewById (R. id. tvResult);} private void setText () {String str; TvResult. setText (""); // clear TextView content // if CbBasketball is selected, add the TvResult content to display if (CbBasketball. isChecked () {str = TvResult. getText (). toString () + CbBasketball. getText (). toString () + ","; TvResult. setText (str);} // if the CbPingpangball is selected, add the TvResult content to display if (CbPingpangball. isChecked () {str = TvResult. getText (). toString () + CbPingpangball. getText (). toString () + ","; TvResult. setText (str);} // if CbFootball is selected, add the TvResult content to display if (CbFootball. isChecked () {str = TvResult. getText (). toString () + CbFootball. getText (). toString (); TvResult. setText (str );}}}