在Android中如何给选中的单选按钮添加下划线?

huangapple go评论233阅读模式
英文:

how to Underline the selected radio button in android?

问题

Sure, here is the translated code portion:

  1. 我有单选按钮。我希望选定的单选按钮被下划线标记。
  2. <RadioGroup
  3. android:weightSum="3"
  4. android:orientation="horizontal"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content">
  7. <RadioButton
  8. android:padding="20dp"
  9. android:layout_weight="1"
  10. android:text="Collections"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:button="@null"
  14. android:background="@drawable/radio_button_background"/>
  15. <RadioButton
  16. android:layout_weight="1"
  17. android:text="Inspections"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:button="@null"
  21. android:background="@drawable/radio_button_background"/>
  22. <RadioButton
  23. android:layout_weight="1"
  24. android:text="Enforcements"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:button="@null"
  28. android:background="@drawable/radio_button_background"/>
  29. </RadioGroup>
  30. 这是我创建的radio_button_background背景。
  31. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  32. <item android:state_checked="true">
  33. <shape android:shape="line">
  34. <stroke
  35. android:width="4dp"
  36. android:color="#0000FF"
  37. android:gravity="bottom"
  38. android:dashWidth="0dp"
  39. android:dashGap="1000dp"/>
  40. </shape>
  41. </item>
  42. <item>
  43. <shape android:shape="line">
  44. <solid android:color="@android:color/transparent"/>
  45. </shape>
  46. </item>
  47. </selector>

Please note that I've only provided the translated code portion as requested, without additional content or responses.

英文:

I have radio buttons. I want when the selected radio button to be underlined.

  1. &lt;RadioGroup
  2. android:weightSum=&quot;3&quot;
  3. android:orientation=&quot;horizontal&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;wrap_content&quot;&gt;
  6. &lt;RadioButton
  7. android:padding=&quot;20dp&quot;
  8. android:layout_weight=&quot;1&quot;
  9. android:text=&quot;Collections&quot;
  10. android:layout_width=&quot;wrap_content&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. android:button=&quot;@null&quot;
  13. android:background=&quot;@drawable/radio_button_background&quot;/&gt;
  14. &lt;RadioButton
  15. android:layout_weight=&quot;1&quot;
  16. android:text=&quot;Inspections&quot;
  17. android:layout_width=&quot;wrap_content&quot;
  18. android:layout_height=&quot;wrap_content&quot;
  19. android:button=&quot;@null&quot;
  20. android:background=&quot;@drawable/radio_button_background&quot;/&gt;
  21. &lt;RadioButton
  22. android:layout_weight=&quot;1&quot;
  23. android:text=&quot;Enforcements&quot;
  24. android:layout_width=&quot;wrap_content&quot;
  25. android:layout_height=&quot;wrap_content&quot;
  26. android:button=&quot;@null&quot;
  27. android:background=&quot;@drawable/radio_button_background&quot;/&gt;
  28. &lt;/RadioGroup&gt;

here is the radio_button_background background I have created.

  1. &lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
  2. &lt;item android:state_checked=&quot;true&quot;&gt;
  3. &lt;shape android:shape=&quot;line&quot;&gt;
  4. &lt;stroke
  5. android:width=&quot;4dp&quot;
  6. android:color=&quot;#0000FF&quot;
  7. android:gravity=&quot;bottom&quot;
  8. android:dashWidth=&quot;0dp&quot;
  9. android:dashGap=&quot;1000dp&quot;/&gt;
  10. &lt;/shape&gt;
  11. &lt;/item&gt;
  12. &lt;item&gt;
  13. &lt;shape android:shape=&quot;line&quot;&gt;
  14. &lt;solid android:color=&quot;@android:color/transparent&quot;/&gt;
  15. &lt;/shape&gt;
  16. &lt;/item&gt;
  17. &lt;/selector&gt;

I want when I select the radio button, the active one is underlined. like below image

在Android中如何给选中的单选按钮添加下划线?

答案1

得分: 1

  1. 为你的单选组和单选按钮分配ID

    1. <RadioGroup
    2. android:id="@+id/radio_group"
    3. android:layout_width="match_parent"
    4. android:layout_height="wrap_content"
    5. android:orientation="horizontal"
    6. android:weightSum="3">
    7. <RadioButton
    8. android:id="@+id/rb_collections"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:layout_weight="1"
    12. android:background="@drawable/te"
    13. android:button="@null"
    14. android:text="Collections"
    15. android:textAlignment="center" />
    16. <RadioButton
    17. android:id="@+id/rb_inspections"
    18. android:layout_width="wrap_content"
    19. android:layout_height="wrap_content"
    20. android:layout_weight="1"
    21. android:button="@null"
    22. android:text="Inspections"
    23. android:textAlignment="center" />
    24. <RadioButton
    25. android:id="@+id/rb_enforcements"
    26. android:layout_width="wrap_content"
    27. android:layout_height="wrap_content"
    28. android:layout_weight="1"
    29. android:button="@null"
    30. android:text="Enforcements"
    31. android:textAlignment="center" />
    32. </RadioGroup>
  2. 创建一个带有底部线的背景,用于选中的按钮

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item
    4. android:left="-40dp"
    5. android:right="-40dp"
    6. android:top="-40dp">
    7. <shape android:shape="rectangle">
    8. <stroke
    9. android:width="2dp"
    10. android:color="@android:color/holo_blue_dark" />
    11. </shape>
    12. </item>
    13. </layer-list>
  3. 每当单选组的状态变化时,将背景设置为选中的单选按钮

    1. val rb = findViewById<RadioGroup>(R.id.radio_group)
    2. val rbCollections = findViewById<RadioButton>(R.id.rb_collections)
    3. val rbInspections = findViewById<RadioButton>(R.id.rb_inspections)
    4. val rbEnforcements = findViewById<RadioButton>(R.id.rb_enforcements)
    5. rb.setOnCheckedChangeListener { group, checkedId ->
    6. // 移除每个按钮的背景
    7. rbCollections.setBackgroundResource(0)
    8. rbInspections.setBackgroundResource(0)
    9. rbEnforcements.setBackgroundResource(0)
    10. // 设置被选中按钮的背景
    11. when(checkedId){
    12. R.id.rb_collections -> rbCollections.setBackgroundResource(R.drawable.bg)
    13. R.id.rb_inspections -> rbInspections.setBackgroundResource(R.drawable.bg)
    14. R.id.rb_enforcements -> rbEnforcements.setBackgroundResource(R.drawable.bg)
    15. }
    16. }
英文:
  1. Give ids to your radion group and radio buttons

    1. &lt;RadioGroup
    2. android:id=&quot;@+id/radio_group&quot;
    3. android:layout_width=&quot;match_parent&quot;
    4. android:layout_height=&quot;wrap_content&quot;
    5. android:orientation=&quot;horizontal&quot;
    6. android:weightSum=&quot;3&quot;&gt;
    7. &lt;RadioButton
    8. android:id=&quot;@+id/rb_collections&quot;
    9. android:layout_width=&quot;wrap_content&quot;
    10. android:layout_height=&quot;wrap_content&quot;
    11. android:layout_weight=&quot;1&quot;
    12. android:background=&quot;@drawable/te&quot;
    13. android:button=&quot;@null&quot;
    14. android:text=&quot;Collections&quot;
    15. android:textAlignment=&quot;center&quot; /&gt;
    16. &lt;RadioButton
    17. android:id=&quot;@+id/rb_inspections&quot;
    18. android:layout_width=&quot;wrap_content&quot;
    19. android:layout_height=&quot;wrap_content&quot;
    20. android:layout_weight=&quot;1&quot;
    21. android:button=&quot;@null&quot;
    22. android:text=&quot;Inspections&quot;
    23. android:textAlignment=&quot;center&quot; /&gt;
    24. &lt;RadioButton
    25. android:id=&quot;@+id/rb_enforcements&quot;
    26. android:layout_width=&quot;wrap_content&quot;
    27. android:layout_height=&quot;wrap_content&quot;
    28. android:layout_weight=&quot;1&quot;
    29. android:button=&quot;@null&quot;
    30. android:text=&quot;Enforcements&quot;
    31. android:textAlignment=&quot;center&quot; /&gt;
    32. &lt;/RadioGroup&gt;
  2. Create a background with the bottom line that you need for selected button

    1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    2. &lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    3. &lt;item
    4. android:left=&quot;-40dp&quot;
    5. android:right=&quot;-40dp&quot;
    6. android:top=&quot;-40dp&quot;&gt;
    7. &lt;shape android:shape=&quot;rectangle&quot;&gt;
    8. &lt;stroke
    9. android:width=&quot;2dp&quot;
    10. android:color=&quot;@android:color/holo_blue_dark&quot; /&gt;
    11. &lt;/shape&gt;
    12. &lt;/item&gt;
    13. &lt;/layer-list&gt;
  3. When ever the state of radio group changes, you set the background to selected radio button

    1. val rb = findViewById&lt;RadioGroup&gt;(R.id.radio_group)
    2. val rbCollections = findViewById&lt;RadioButton&gt;(R.id.rb_collections)
    3. val rbInspections = findViewById&lt;RadioButton&gt;(R.id.rb_inspections)
    4. val rbEnforcements = findViewById&lt;RadioButton&gt;(R.id.rb_enforcements)
    5. rb.setOnCheckedChangeListener { group, checkedId -&gt;
    6. // remove background of every button
    7. rbCollections.setBackgroundResource(0)
    8. rbInspections.setBackgroundResource(0)
    9. rbEnforcements.setBackgroundResource(0)
    10. // set background of button which is checked
    11. when(checkedId){
    12. R.id.rb_collections -&gt; rbCollections.setBackgroundResource(R.drawable.bg)
    13. R.id.rb_inspections -&gt; rbInspections.setBackgroundResource(R.drawable.bg)
    14. R.id.rb_enforcements -&gt; rbEnforcements.setBackgroundResource(R.drawable.bg)
    15. }
    16. }

huangapple
  • 本文由 发表于 2023年8月11日 01:38:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定