RadioGroup实现单选框的多行排列

前端之家收集整理的这篇文章主要介绍了RadioGroup实现单选框的多行排列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

RadioGroup的使用非常简单,只是一般情况下,只能是横向排列或竖向排列.如果让多横排列的的就不是那么简单的了。

也许有童鞋该说了,将RadioButton写到LineLayout中不久行了吗?经过检验确实可以那样做,刚开始我也是这样做到.不过运行起来发现了了一个bug---单选按钮不在是单选了.而且选择事件不会被监听到.这就要求我们去想办法了.其实实现起来也不难.只要多用几个RadioGroup就可以了(要在代码中处理一些事件)。

代码

1.xml中的布局:

  1. <RelativeLayout
  2. android:id="@+id/main_tab_container"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:paddingTop="30dp">
  6.  
  7. <RadioGroup
  8. android:id="@+id/radio1"
  9. android:layout_width="match_parent"
  10. android:layout_height="60dp"
  11. android:layout_margin="5dp"
  12. android:orientation="horizontal">
  13.  
  14. <RadioButton
  15. android:id="@+id/rb_1"
  16. android:layout_width="0dp"
  17. android:layout_height="wrap_content"
  18. android:layout_weight="1"
  19. android:textSize="@dimen/RB_text_size"
  20. android:text="GBP英镑" />
  21.  
  22. <RadioButton
  23. android:id="@+id/rb_2"
  24. android:layout_width="0dp"
  25. android:layout_height="wrap_content"
  26. android:layout_weight="1"
  27. android:textSize="@dimen/RB_text_size"
  28. android:text="HKD港元" />
  29.  
  30. <RadioButton
  31. android:id="@+id/rb_3"
  32. android:layout_width="0dp"
  33. android:layout_height="wrap_content"
  34. android:layout_weight="1"
  35. android:textSize="@dimen/RB_text_size"
  36. android:text="USD美元Ԫ" />
  37. </RadioGroup>
  38.  
  39. <RadioGroup
  40. android:id="@+id/radio2"
  41. android:layout_width="match_parent"
  42. android:layout_height="60dp"
  43. android:layout_below="@+id/radio1"
  44. android:layout_margin="5dp"
  45. android:orientation="horizontal">
  46.  
  47. <RadioButton
  48. android:id="@+id/rb_4"
  49. android:layout_width="0dp"
  50. android:layout_height="wrap_content"
  51. android:layout_weight="1"
  52. android:textSize="@dimen/RB_text_size"
  53. android:text="CHF瑞士法郎" />
  54.  
  55. <RadioButton
  56. android:id="@+id/rb_5"
  57. android:layout_width="0dp"
  58. android:layout_height="wrap_content"
  59. android:layout_weight="1"
  60. android:textSize="@dimen/RB_text_size"
  61. android:text="SGD新加坡元" />
  62.  
  63. <RadioButton
  64. android:id="@+id/rb_6"
  65. android:layout_width="0dp"
  66. android:layout_height="wrap_content"
  67. android:layout_weight="1"
  68. android:textSize="@dimen/RB_text_size"
  69. android:text="SEK瑞典克朗" />
  70. </RadioGroup>
  71.  
  72. <RadioGroup
  73. android:id="@+id/radio3"
  74. android:layout_width="match_parent"
  75. android:layout_height="60dp"
  76. android:layout_below="@+id/radio2"
  77. android:layout_margin="5dp"
  78. android:orientation="horizontal">
  79.  
  80. <RadioButton
  81. android:id="@+id/rb_7"
  82. android:layout_width="0dp"
  83. android:layout_height="wrap_content"
  84. android:layout_weight="1"
  85. android:textSize="@dimen/RB_text_size"
  86. android:text="JPY日元" />
  87.  
  88. <RadioButton
  89. android:id="@+id/rb_8"
  90. android:layout_width="0dp"
  91. android:layout_height="wrap_content"
  92. android:layout_weight="1"
  93. android:textSize="@dimen/RB_text_size"
  94. android:text="CAD加拿大元Ԫ" />
  95.  
  96. <RadioButton
  97. android:id="@+id/rb_9"
  98. android:layout_width="0dp"
  99. android:layout_height="wrap_content"
  100. android:layout_weight="1"
  101. android:textSize="@dimen/RB_text_size"
  102. android:text="AUD澳大利亚元" />
  103. </RadioGroup>
  104.  
  105. <RadioGroup
  106. android:id="@+id/radio4"
  107. android:layout_width="match_parent"
  108. android:layout_height="60dp"
  109. android:layout_below="@+id/radio3"
  110. android:layout_margin="5dp"
  111. android:orientation="horizontal">
  112.  
  113. <RadioButton
  114. android:id="@+id/rb_10"
  115. android:layout_width="wrap_content"
  116. android:layout_height="wrap_content"
  117. android:textSize="@dimen/RB_text_size"
  118. android:text="EOR欧元Ԫ" />
  119.  
  120. </RadioGroup>
  121.  
  122. </RelativeLayout>

这样就实现了多行布局,这只是我布局中的一部分,其中 android:textSize=”@dimen/RB_text_size” 为自己定义的字体大小.

2.activity中的使用以及处理:

  1. public class SelectMoneyActivity extends BaseActivity {
  2.  
  3. String strBtnSelected = ""; //记录选择的是哪个选项
  4.  
  5. private RadioGroup rg1,rg2,rg3,rg4;
  6. private RadioButton rb_1,rb_2,rb_3,rb_4,rb_5,rb_6,rb_7,rb_8,rb_9,rb_10;
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_select_money);
  12.  
  13. initView();
  14.  
  15. }
  16.  
  17. private void initView() {
  18.  
  19. rg1 = (RadioGroup) findViewById(R.id.radio1);
  20. rg2 = (RadioGroup) findViewById(R.id.radio2);
  21. rg3 = (RadioGroup) findViewById(R.id.radio3);
  22. rg4 = (RadioGroup) findViewById(R.id.radio4);
  23.  
  24. rb_1 = (RadioButton) findViewById(R.id.rb_1);
  25. rb_2 = (RadioButton) findViewById(R.id.rb_2);
  26. rb_3 = (RadioButton) findViewById(R.id.rb_3);
  27. rb_4 = (RadioButton) findViewById(R.id.rb_4);
  28. rb_5 = (RadioButton) findViewById(R.id.rb_5);
  29. rb_6 = (RadioButton) findViewById(R.id.rb_6);
  30. rb_7 = (RadioButton) findViewById(R.id.rb_7);
  31. rb_8 = (RadioButton) findViewById(R.id.rb_8);
  32. rb_9 = (RadioButton) findViewById(R.id.rb_9);
  33. rb_10 = (RadioButton) findViewById(R.id.rb_10);
  34.  
  35. btn_back = (Button) findViewById(R.id.btn_back);
  36. btn_next = (Button) findViewById(R.id.btn_next);
  37.  
  38. //创建监听器,为每个RadioButton注册监听
  39.  
  40. BtnSelected btnSelected1 = new BtnSelected("1");
  41. BtnSelected btnSelected2 = new BtnSelected("2");
  42. BtnSelected btnSelected3 = new BtnSelected("3");
  43. BtnSelected btnSelected4 = new BtnSelected("4");
  44. BtnSelected btnSelected5 = new BtnSelected("5");
  45. BtnSelected btnSelected6 = new BtnSelected("6");
  46. BtnSelected btnSelected7 = new BtnSelected("7");
  47. BtnSelected btnSelected8 = new BtnSelected("8");
  48. BtnSelected btnSelected9 = new BtnSelected("9");
  49. BtnSelected btnSelected10 = new BtnSelected("10");
  50.  
  51. rb_1.setOnClickListener(btnSelected1);
  52. rb_2.setOnClickListener(btnSelected2);
  53. rb_3.setOnClickListener(btnSelected3);
  54. rb_4.setOnClickListener(btnSelected4);
  55. rb_5.setOnClickListener(btnSelected5);
  56. rb_6.setOnClickListener(btnSelected6);
  57. rb_7.setOnClickListener(btnSelected7);
  58. rb_8.setOnClickListener(btnSelected8);
  59. rb_9.setOnClickListener(btnSelected9);
  60. rb_10.setOnClickListener(btnSelected10);
  61.  
  62. //点击事件的监听器
  63. public class BtnSelected implements View.OnClickListener {
  64.  
  65. private String btnId;
  66.  
  67. public BtnSelected(String str) {
  68. btnId = str;
  69. }
  70.  
  71. @Override
  72. public void onClick(View v) {
  73. strBtnSelected = btnId;  //选择的某一项
  74. isSelect = true;
  75. //点击了第一行 ,就把另外行的点击项清空
  76. if (btnId.equals("1") || btnId.equals("2") || btnId.equals("3")) {
  77.  
  78. rg2.clearCheck();
  79. rg3.clearCheck();
  80. rg4.clearCheck();
  81. } else if (btnId.equals("4") || btnId.equals("5") || btnId.equals("6")) {
  82. rg1.clearCheck();
  83. rg3.clearCheck();
  84. rg4.clearCheck();
  85. } else if (btnId.equals("7") || btnId.equals("8") || btnId.equals("9")) {
  86. rg1.clearCheck();
  87. rg2.clearCheck();
  88. rg4.clearCheck();
  89. } else {
  90. rg1.clearCheck();
  91. rg2.clearCheck();
  92. rg3.clearCheck();
  93. }
  94. }
  95. }
  96. }

已经搞定.还有一种方法就是自定义RadioGroup实现,不过这种有点复杂.我还是下班回家了.

补充:

使用RadioGroup.setcheck(RadioButton的id)初始化默认选中A按钮,但是监听不会执行的问题

解决因为已经给A按钮在布局中设置了check=”true”; 将这个属性去掉就会执行监听了.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的Android相关文章