Definition of Android CheckBox control and event monitoring

Source: Internet
Author: User

http://www.beijibear.com/index.php?aid=336

The definition of the Android CheckBox control and event monitoring, this example implements the CheckBox control definition and click event Monitoring and display the results, the effect is as follows:

The definition of the CheckBox control, Main.xml content is as follows:

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
  3. android:orientation= "Vertical"
  4. Android:layout_width= "Fill_parent"
  5. android:layout_height= "Fill_parent"
  6. >
  7. <edittext
  8. Android:id= "@+id/edittext1"
  9. Android:layout_width= "Fill_parent"
  10. android:layout_height= "Wrap_content"
  11. android:text= "Please select"
  12. />
  13. <checkbox
  14. Android:id= "@+id/beijing"
  15. Android:layout_width= "Wrap_content"
  16. android:layout_height= "Wrap_content"
  17. android:text= "Beijing"
  18. />
  19. <checkbox
  20. Android:id= "@+id/shanghai"
  21. Android:layout_width= "Wrap_content"
  22. android:layout_height= "Wrap_content"
  23. android:text= "Shanghai"
  24. />
  25. <checkbox
  26. Android:id= "@+id/shenzhen"
  27. Android:layout_width= "Wrap_content"
  28. android:layout_height= "Wrap_content"
  29. android:text= "Shenzhen"
  30. />
  31. </LinearLayout>

Activity Checkboxtest.java content is as follows:

  1. Package checkbox.pack;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.widget.CheckBox;
  5. Import Android.widget.CompoundButton;
  6. Import Android.widget.EditText;
  7. public class Checkboxtest extends Activity {
  8. Declaring a Control object
  9. CheckBox Beijing=null;
  10. CheckBox Shanghai=null;
  11. CheckBox Shenzhen=null;
  12. EditText Edittext1=null;
  13. @Override
  14. public void OnCreate (Bundle savedinstancestate) {
  15. Super.oncreate (savedinstancestate);
  16. Setcontentview (R.layout.main);
  17. To get the object that represents the control through the control's ID
  18. Beijing= (CheckBox) Findviewbyid (r.id.beijing);
  19. Shanghai= (CheckBox) Findviewbyid (R.id.shanghai);
  20. Shenzhen= (CheckBox) Findviewbyid (R.id.shenzhen);
  21. edittext1= (EditText) Findviewbyid (R.ID.EDITTEXT1);
  22. Set up event monitoring for a checkbox
  23. Beijing.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {
  24. @Override
  25. public void OnCheckedChanged (Compoundbutton buttonview,
  26. Boolean isChecked) {
  27. TODO auto-generated Method Stub
  28. if (isChecked) {
  29. Edittext1.settext (Buttonview.gettext () + "checked");
  30. }else{
  31. Edittext1.settext (Buttonview.gettext () + "uncheck");
  32. }
  33. }
  34. });
  35. Shanghai.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {
  36. @Override
  37. public void OnCheckedChanged (Compoundbutton buttonview,
  38. Boolean isChecked) {
  39. TODO auto-generated Method Stub
  40. if (isChecked) {
  41. Edittext1.settext (Buttonview.gettext () + "checked");
  42. }else{
  43. Edittext1.settext (Buttonview.gettext () + "uncheck");
  44. }
  45. }
  46. });
  47. Shenzhen.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {
  48. @Override
  49. public void OnCheckedChanged (Compoundbutton buttonview,
  50. Boolean isChecked) {
  51. TODO auto-generated Method Stub
  52. if (isChecked) {
  53. Edittext1.settext (Buttonview.gettext () + "checked");
  54. }else{
  55. Edittext1.settext (Buttonview.gettext () + "uncheck");
  56. }
  57. }
  58. });
  59. }
  60. }

Definition of Android CheckBox control and event monitoring

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.