Multiple select button (CheckBox), multiple select button checkbox

Source: Internet
Author: User

Multiple select button (CheckBox), multiple select button checkbox

Today we will introduce the Checkbox multi-choice box:

1. Activity

// Check box, [basic control] --- status switch control CompoundButton and its subclass CheckBox, RadioButton, ToggleButton, switch event listening and scenario use public class CheckBoxActivity extends Activity implements CompoundButton. onCheckedChangeListener {private Context context; private CheckBox sleepCheckBox; private CheckBox dadoudouCheckBox; private CheckBox gameCheckBox; private CheckBox shoppingCheckBox; private CheckBox filmCheckBox; private CheckBox sportChec KBox; private Button submitButton; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. check_box); init (); addAction ();} private void init () {context = this; sleepCheckBox = (CheckBox) findViewById (R. id. sleepCheckBoxId); dadoudouCheckBox = (CheckBox) findViewById (R. id. dadoudouCheckBoxId); gameCheckBox = (CheckBox) findViewById (R. Id. gameCheckBoxId); shoppingCheckBox = (CheckBox) findViewById (R. id. shoppingCheckBoxId); filmCheckBox = (CheckBox) findViewById (R. id. filmCheckBoxId); sportCheckBox = (CheckBox) findViewById (R. id. sportCheckBoxId); submitButton = (Button) findViewById (R. id. submitButtonId);} private void addAction () {sleepCheckBox. setOnCheckedChangeListener (this); dadoudouCheckBox. setOnCheckedChangeListener (this); gameC HeckBox. setOnCheckedChangeListener (this); shoppingCheckBox. setOnCheckedChangeListener (this); filmCheckBox. setOnCheckedChangeListener (this); sportCheckBox. setOnCheckedChangeListener (this); submitButton. setOnClickListener (new View. onClickListener () {public void onClick (View v) {// String constant // StringBuffer String variable (thread safety) // StringBuilder String variable (non-thread safety) stringBuilder sb = new StringBuilder ("your interest is :");// MyStringBuilder. Insert (6, "Beautiful"); // MyStringBuilder. Remove (5, 7); // MyStringBuilder. Replace ('! ','? '); // Code example specifies that the MyStringBuilder object can be expanded to a maximum of 25 white spaces. // StringBuilderMyStringBuilder = new StringBuilder ("Hello World! ", 25); if (sleepCheckBox. isChecked () {sb. append ("sleeping");} if (dadoudouCheckBox. isChecked () {sb. append ("doubebean");} if (gameCheckBox. isChecked () {sb. append ("game");} if (shoppingCheckBox. isChecked () {sb. append ("shopping");} if (filmCheckBox. isChecked () {sb. append ("movie");} if (sportCheckBox. isChecked () {sb. append ("motion");} Toast. makeText (context, sb. toString (), Toast. LENGTH_LONG ). show () ;}}) ;}@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {int id = buttonView. getId (); switch (id) {case R. id. sleepCheckBoxId: if (isChecked) {Toast. makeText (context, "you selected \" Sleep \ "", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (context, "you have canceled \" Sleeping \ "", Toast. LENGTH_SHORT ). show ();} break; case R. id. dadoudouCheckBoxId: if (isChecked) {Toast. makeText (context, "you selected \" Doudou \ "", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (context, "you have deselected \" Doudou \ "", Toast. LENGTH_SHORT ). show ();} break; case R. id. gameCheckBoxId: if (isChecked) {Toast. makeText (context, "you selected \" game \ "", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (context, "you have deselected \" game \ "", Toast. LENGTH_SHORT ). show ();} break; case R. id. shoppingCheckBoxId: if (isChecked) {Toast. makeText (context, "you selected \" Shopping \ "", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (context, "you have deselected \" Shopping \ "", Toast. LENGTH_SHORT ). show ();} break; case R. id. filmCheckBoxId: if (isChecked) {Toast. makeText (context, "you selected \" movie \ "", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (context, "you have deselected \" movie \ "", Toast. LENGTH_SHORT ). show ();} break; case R. id. sportCheckBoxId: if (isChecked) {Toast. makeText (context, "you selected \" \ "", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (context, "you have deselected \" \ ", Toast. LENGTH_SHORT ). show () ;}break ;}}}

2. the xml file is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Check box page --> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: padding = "5dp"> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Interest:" android: textSize = "20sp"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <CheckBox android: id = "@ + id/sleepCheckBoxId" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "sleeping"/> <CheckBox android: id = "@ + id/dadoudouCheckBoxId" android: layout_width = "85dp" android: layout_height = "wrap_content" android: text = "Doudou"/> <CheckBox android: id = "@ + id/gameCheckBoxId" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "game"/> </LinearLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <CheckBox android: id = "@ + id/shoppingCheckBoxId" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "shopping"/> <CheckBox android: id = "@ + id/filmCheckBoxId" android: layout_width = "85dp" android: layout_height = "wrap_content" android: text = "movie"/> <CheckBox android: id = "@ + id/sportCheckBoxId" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout> <Button android: id = "@ + id/submitButtonId" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "right" android: text = "OK" android: textSize = "20sp"/> </LinearLayout>

3. as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.