• Android RadioGroup实现多行显示,并保持单选


    公司项目最近有个这样的需求,要求实现【多个文本,多行显示,且同时只能选中一个】。设计图效果如下:

    看上去很简单,使用 RadioGroup + LinearLayout + RadioButton 快速实现:

    1. <RadioGroup
    2. android:id="@+id/rg"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:layout_marginLeft="@dimen/SIZE_18"
    6. android:layout_marginTop="@dimen/SIZE_9"
    7. android:layout_marginRight="@dimen/SIZE_18"
    8. android:layout_marginBottom="@dimen/SIZE_20">
    9. <LinearLayout
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:orientation="horizontal">
    13. <RadioButton
    14. android:id="@+id/RadioButton1"
    15. style="@style/fontStyle"
    16. android:layout_width="wrap_content"
    17. android:layout_height="28dp"
    18. android:background="@drawable/bg_button_bg"
    19. android:button="@null"
    20. android:paddingLeft="@dimen/SIZE_10"
    21. android:paddingRight="@dimen/SIZE_10"
    22. android:text="@string/str_abnormal_function"
    23. android:textColor="@drawable/text_color_select"
    24. android:textSize="@dimen/SP_SIZE_12">RadioButton>
    25. <RadioButton
    26. android:id="@+id/RadioButton2"
    27. android:layout_width="wrap_content"
    28. android:layout_height="28dp"
    29. android:layout_marginLeft="@dimen/SIZE_10"
    30. android:autoSizeMaxTextSize="18sp"
    31. android:autoSizeMinTextSize="6sp"
    32. android:autoSizeStepGranularity="1sp"
    33. android:autoSizeTextType="uniform"
    34. android:background="@drawable/bg_button_bg"
    35. android:button="@null"
    36. android:paddingLeft="@dimen/SIZE_10"
    37. android:paddingRight="@dimen/SIZE_10"
    38. android:text="@string/str_experience_problem"
    39. android:textColor="@drawable/text_color_select"
    40. android:textSize="@dimen/SP_SIZE_12">RadioButton>
    41. <RadioButton
    42. android:id="@+id/RadioButton3"
    43. style="@style/fontStyle"
    44. android:layout_width="wrap_content"
    45. android:layout_height="28dp"
    46. android:layout_marginLeft="@dimen/SIZE_10"
    47. android:background="@drawable/bg_button_bg"
    48. android:button="@null"
    49. android:paddingLeft="@dimen/SIZE_10"
    50. android:paddingRight="@dimen/SIZE_10"
    51. android:text="@string/str_new_feature_suggestion"
    52. android:textColor="@drawable/text_color_select"
    53. android:textSize="@dimen/SP_SIZE_12">RadioButton>
    54. LinearLayout>
    55. <RadioButton
    56. android:id="@+id/RadioButton4"
    57. style="@style/fontStyle"
    58. android:layout_width="wrap_content"
    59. android:layout_height="28dp"
    60. android:layout_marginTop="@dimen/SIZE_9"
    61. android:background="@drawable/bg_button_bg"
    62. android:button="@null"
    63. android:paddingLeft="@dimen/SIZE_10"
    64. android:paddingRight="@dimen/SIZE_10"
    65. android:text="@string/str_type_other"
    66. android:textColor="@drawable/text_color_select"
    67. android:textSize="@dimen/SP_SIZE_12">RadioButton>
    68. RadioGroup>

    跑起来后,发现并没有实现单选的效果,究其根本,观其RadioGroup源码:

    1. @Override
    2. public void addView(View child, int index, ViewGroup.LayoutParams params) {
    3. if (child instanceof RadioButton) {
    4. final RadioButton button = (RadioButton) child;
    5. if (button.isChecked()) {
    6. mProtectFromCheckedChange = true;
    7. if (mCheckedId != -1) {
    8. setCheckedStateForView(mCheckedId, false);
    9. }
    10. mProtectFromCheckedChange = false;
    11. setCheckedId(button.getId());
    12. }
    13. }
    14. super.addView(child, index, params);
    15. }

    RadioGroup在添加子view时,仅判断了其是否是RadioButton,故LinearLayout并不符合这一条件,要想实现内嵌LinearLayout+RadioButton,只能自定义RadioGroup,并重写addView方法。本文使用了另一方式解决这个问题:

    使用多个RadioGroup内嵌RadioButton,当其中1个RadioGroup中的RadioButton被选中时,清除其他RadioGroup的选中效果,代码如下:

    1. <RadioGroup
    2. android:id="@+id/rg"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:layout_marginLeft="@dimen/SIZE_18"
    6. android:layout_marginTop="@dimen/SIZE_9"
    7. android:layout_marginRight="@dimen/SIZE_18"
    8. android:orientation="horizontal">
    9. <RadioButton
    10. android:id="@+id/RadioButton1"
    11. style="@style/fontStyle"
    12. android:layout_width="wrap_content"
    13. android:layout_height="28dp"
    14. android:background="@drawable/bg_button_bg"
    15. android:button="@null"
    16. android:paddingLeft="@dimen/SIZE_10"
    17. android:paddingRight="@dimen/SIZE_10"
    18. android:text="@string/str_abnormal_function"
    19. android:textColor="@drawable/text_color_select"
    20. android:textSize="@dimen/SP_SIZE_12" />
    21. <RadioButton
    22. android:id="@+id/RadioButton2"
    23. android:layout_width="wrap_content"
    24. android:layout_height="28dp"
    25. android:layout_marginLeft="@dimen/SIZE_10"
    26. android:autoSizeMaxTextSize="18sp"
    27. android:autoSizeMinTextSize="6sp"
    28. android:autoSizeStepGranularity="1sp"
    29. android:autoSizeTextType="uniform"
    30. android:background="@drawable/bg_button_bg"
    31. android:button="@null"
    32. android:paddingLeft="@dimen/SIZE_10"
    33. android:paddingRight="@dimen/SIZE_10"
    34. android:text="@string/str_experience_problem"
    35. android:textColor="@drawable/text_color_select"
    36. android:textSize="@dimen/SP_SIZE_12" />
    37. <RadioButton
    38. android:id="@+id/RadioButton3"
    39. style="@style/fontStyle"
    40. android:layout_width="wrap_content"
    41. android:layout_height="28dp"
    42. android:layout_marginLeft="@dimen/SIZE_10"
    43. android:background="@drawable/bg_button_bg"
    44. android:button="@null"
    45. android:paddingLeft="@dimen/SIZE_10"
    46. android:paddingRight="@dimen/SIZE_10"
    47. android:text="@string/str_new_feature_suggestion"
    48. android:textColor="@drawable/text_color_select"
    49. android:textSize="@dimen/SP_SIZE_12" />
    50. RadioGroup>
    51. <RadioGroup
    52. android:id="@+id/rgTow"
    53. android:layout_width="match_parent"
    54. android:layout_height="match_parent"
    55. android:layout_marginLeft="@dimen/SIZE_18"
    56. android:layout_marginRight="@dimen/SIZE_18"
    57. android:layout_marginBottom="@dimen/SIZE_20">
    58. <RadioButton
    59. android:id="@+id/RadioButton4"
    60. style="@style/fontStyle"
    61. android:layout_width="wrap_content"
    62. android:layout_height="28dp"
    63. android:layout_marginTop="@dimen/SIZE_9"
    64. android:background="@drawable/bg_button_bg"
    65. android:button="@null"
    66. android:paddingLeft="@dimen/SIZE_10"
    67. android:paddingRight="@dimen/SIZE_10"
    68. android:text="@string/str_type_other"
    69. android:textColor="@drawable/text_color_select"
    70. android:textSize="@dimen/SP_SIZE_12" />
    71. RadioGroup>

    实现逻辑:

    1. private fun init(){
    2. mViewBind.rg.setOnCheckedChangeListener(OneCheckedChange())
    3. mViewBind.rgTow.setOnCheckedChangeListener(TowCheckedChange())
    4. }
    5. inner class OneCheckedChange : OnCheckedChangeListener {
    6. override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
    7. // 清除另一RadioGroup选中状态
    8. mViewBind.rgTow.setOnCheckedChangeListener(null)
    9. mViewBind.rgTow.clearCheck()
    10. mViewBind.rgTow.setOnCheckedChangeListener(TowCheckedChange())
    11. // 根据 ID 判断选择的按钮
    12. mtype = when (checkedId) {
    13. R.id.RadioButton1 -> 1
    14. R.id.RadioButton2 -> 2
    15. R.id.RadioButton3 -> 3
    16. else -> 0
    17. }
    18. }
    19. }
    20. inner class TowCheckedChange : OnCheckedChangeListener{
    21. override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
    22. mViewBind.rg.setOnCheckedChangeListener(null)
    23. mViewBind.rg.clearCheck()
    24. mViewBind.rg.setOnCheckedChangeListener(OneCheckedChange())
    25. // 根据 ID 判断选择的按钮
    26. if(checkedId == R.id.RadioButton4){
    27. mtype =4
    28. }
    29. }
    30. }

    至此实现【多个文本,多行显示,且同时只能选中一个】效果。

  • 相关阅读:
    Nest.js项目小结1
    9.项目细节调整
    [车联网安全自学篇] 五十二. Android安全之本地存储敏感信息挖掘【二】
    HTTP中的强缓存与协商缓存
    如何使用做一个弹幕效果
    vue3 配置生产和开发 非vite
    利用 fail2ban 保护 SSH 服务器
    AI量化(代码):深度强化学习DRL应用于金融量化
    iptables-参数-A和-I
    java计算机毕业设计摄影网上预约管理系统MyBatis+系统+LW文档+源码+调试部署
  • 原文地址:https://blog.csdn.net/sziitjin/article/details/140990070