RadioButton and CheckBox, radiobutton

Source: Internet
Author: User

RadioButton and CheckBox, radiobutton

I have been engaged in database development for a long time. Forget it. Because I have been using a small language (PowerBuilder), let's talk about these two most common controls!

RadioButton (single choice) and CheckBox (multiple choice)
  • RadioButton must be divided by group, instead of CheckBox, so you can play freely;
  •  
  • On the code, familiarize yourself
  •  
  • Code
    1. PublicclassCheckboxAndRadioBoxActivityextendsAppCompatActivityimplementsCompoundButton. OnCheckedChangeListener,
    2. RadioGroup.OnCheckedChangeListener{
    3. privateRadioGroup rg_sex;
    4. privateCheckBox cb_swimming;
    5. privateCheckBox cb_running;
    6. privateCheckBox cb_study;
    7. privateList<String> hobby =newArrayList<>();
    8. @Override
    9. protectedvoid onCreate(@NullableBundle savedInstanceState){
    10. super.onCreate(savedInstanceState);
    11. setContentView(R.layout.activity_checkbox_and_radiobox);
    12. rg_sex =(RadioGroup) findViewById(R.id.rg_sex);
    13. cb_swimming =(CheckBox) findViewById(R.id.cb_swimming);
    14. cb_running =(CheckBox) findViewById(R.id.cb_running);
    15. cb_study =(CheckBox) findViewById(R.id.cb_study);
    16. rg_sex.setOnCheckedChangeListener(this);
    17. cb_swimming.setOnCheckedChangeListener(this);
    18. cb_running.setOnCheckedChangeListener(this);
    19. cb_study.setOnCheckedChangeListener(this);
    20. }
    21. @Override
    22. publicvoid onCheckedChanged(CompoundButton buttonView,boolean isChecked){
    23. switch(buttonView.getId()){
    24. case R.id.cb_swimming:
    25. if(isChecked){
    26. Holobby. add ("swimming ");
    27. }else{
    28. Holobby. remove ("swimming ");
    29. }
    30. break;
    31. case R.id.cb_running:
    32. if(isChecked){
    33. Holobby. add ("running ");
    34. }else{
    35. Holobby. remove ("running ");
    36. }
    37. break;
    38. case R.id.cb_study:
    39. if(isChecked){
    40. Holobby. add ("Learning ");
    41. }else{
    42. Holobby. remove ("Learning ");
    43. }
    44. break;
    45. }
    46. String str="";
    47. for(int i =0; i < hobby.size(); i++){
    48. if(i==0){
    49. str = hobby.get(0);
    50. }else{
    51. str +=","+hobby.get(i);
    52. }
    53. }
    54. Toast. makeText (getApplicationContext (), "Hobbies:" + str, Toast. LENGTH_SHORT). show ();
    55. }
    56. @Override
    57. publicvoid onCheckedChanged(RadioGroup group,int checkedId){
    58. switch(checkedId){
    59. case R.id.rb_man:
    60. Toast. makeText (getApplicationContext (), "Gender: male", Toast. LENGTH_SHORT). show ();
    61. break;
    62. case R.id.rb_woman:
    63. Toast. makeText (getApplicationContext (), "Gender: female", Toast. LENGTH_SHORT). show ();
    64. break;
    65. }
    66. }
    67. }
     
  • What if the OnCheckedChangeListener of the RadioGroup and CompoundButton conflicts? Their methods are onCheckedChanged. Here I use Activity to implement the above two interfaces at the same time. According to different parameters, RadioGroup and CompoundButton know that they are calling their own interface methods. Although the names are the same, but the parameters are different. here we need to understand two concepts.


  • From Weizhi note (Wiz)



    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.