当前位置:   article > 正文

android中的单选和多选按钮的使用

android 单选

1.布局文件:

  1. <RadioGroup android:layout_width="wrap_content"
  2. android:layout_height="wrap_content"
  3. android:orientation="vertical"
  4. android:id="@+id/radioGroup">
  5. <RadioButton android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"
  7. android:id="@+id/radio1"
  8. android:text="@string/female"/>
  9. <RadioButton android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:id="@+id/radio2"
  12. android:text="@string/male"/>
  13. </RadioGroup>
  14. <CheckBox android:id="@+id/singBox"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="@string/sing"/>
  18. <CheckBox android:id="@+id/runBox"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="@string/run"/>
  22. <CheckBox android:id="@+id/danceBox"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="@string/dance"/>

2.activity

  1. package com.example.android1;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.CheckBox;
  9. import android.widget.CompoundButton;
  10. import android.widget.LinearLayout;
  11. import android.widget.RadioButton;
  12. import android.widget.RadioGroup;
  13. import android.widget.Toast;
  14. public class LinearLayOut extends Activity
  15. {
  16. private RadioGroup radioGroup;
  17. private RadioButton radio1,radio2;
  18. private CheckBox runBox,singBox,danceBox;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. Button button=(Button)findViewById(R.id.button);
  25. button.setOnClickListener(new MyButtonListener());//绑定监听器
  26. radio1=(RadioButton)findViewById(R.id.radio1);
  27. radio2=(RadioButton)findViewById(R.id.radio2);
  28. radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
  29. runBox=(CheckBox)findViewById(R.id.runBox);
  30. singBox=(CheckBox)findViewById(R.id.singBox);
  31. danceBox=(CheckBox)findViewById(R.id.danceBox);
  32. //单选按钮监听器
  33. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
  34. {
  35. @Override
  36. public void onCheckedChanged(RadioGroup group, int checkedId)
  37. {
  38. // TODO Auto-generated method stub
  39. if(radio1.getId()==checkedId)
  40. {
  41. System.out.println("女");
  42. // Toast.makeText(LinearLayout.class, "选择了女", Toast.LENGTH_SHORT).show();
  43. Toast.makeText(LinearLayOut.this, "选择了女", Toast.LENGTH_SHORT).show();
  44. }
  45. else if(radio2.getId()==checkedId)
  46. {
  47. System.out.println("男");
  48. }
  49. }
  50. });
  51. //复选框监听器,每一个checkbox都需要一个
  52. runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
  53. {
  54. @Override
  55. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
  56. {
  57. if(isChecked)
  58. {
  59. System.out.println("runBox is selected");
  60. }
  61. else
  62. {
  63. System.out.println("runBox is unselected");
  64. }
  65. }
  66. });
  67. singBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
  68. {
  69. @Override
  70. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
  71. {
  72. if(isChecked)
  73. {
  74. System.out.println("singBox is selected");
  75. }
  76. else
  77. {
  78. System.out.println("singBox is unselected");
  79. }
  80. }
  81. });
  82. danceBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
  83. {
  84. @Override
  85. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
  86. {
  87. if(isChecked)
  88. {
  89. System.out.println("danceBox is selected");
  90. }
  91. else
  92. {
  93. System.out.println("danceBox is unselected");
  94. }
  95. }
  96. });
  97. }
  98. class MyButtonListener implements OnClickListener
  99. {
  100. @Override
  101. public void onClick(View v)
  102. {
  103. Intent intent=new Intent();
  104. intent.setClass(LinearLayOut.this, TableLayout.class);
  105. LinearLayOut.this.startActivity(intent);
  106. }
  107. }
  108. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/422029
推荐阅读
相关标签
  

闽ICP备14008679号