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

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

how to Underline the selected radio button in android?

问题

Sure, here is the translated code portion:

我有单选按钮。我希望选定的单选按钮被下划线标记。

<RadioGroup
    android:weightSum="3"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
        android:padding="20dp"
        android:layout_weight="1"
        android:text="Collections"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@null"
        android:background="@drawable/radio_button_background"/>

    <RadioButton
        android:layout_weight="1"
        android:text="Inspections"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@null"
        android:background="@drawable/radio_button_background"/>

    <RadioButton
        android:layout_weight="1"
        android:text="Enforcements"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@null"
        android:background="@drawable/radio_button_background"/>
</RadioGroup>

这是我创建的radio_button_background背景。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape android:shape="line">
            <stroke
                android:width="4dp"
                android:color="#0000FF"
                android:gravity="bottom"
                android:dashWidth="0dp"
                android:dashGap="1000dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="line">
            <solid android:color="@android:color/transparent"/>
        </shape>
    </item>
</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.

&lt;RadioGroup
            android:weightSum=&quot;3&quot;
            android:orientation=&quot;horizontal&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;&gt;

            &lt;RadioButton
                android:padding=&quot;20dp&quot;
                android:layout_weight=&quot;1&quot;
                android:text=&quot;Collections&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:button=&quot;@null&quot;
                android:background=&quot;@drawable/radio_button_background&quot;/&gt;

            &lt;RadioButton

                android:layout_weight=&quot;1&quot;
                android:text=&quot;Inspections&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:button=&quot;@null&quot;
                android:background=&quot;@drawable/radio_button_background&quot;/&gt;

            &lt;RadioButton
                android:layout_weight=&quot;1&quot;
                android:text=&quot;Enforcements&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:button=&quot;@null&quot;
                android:background=&quot;@drawable/radio_button_background&quot;/&gt;

        &lt;/RadioGroup&gt;

here is the radio_button_background background I have created.

&lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;item android:state_checked=&quot;true&quot;&gt;
        &lt;shape android:shape=&quot;line&quot;&gt;
            &lt;stroke
                android:width=&quot;4dp&quot;
                android:color=&quot;#0000FF&quot;
                android:gravity=&quot;bottom&quot;
                android:dashWidth=&quot;0dp&quot;
                android:dashGap=&quot;1000dp&quot;/&gt;
        &lt;/shape&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;shape android:shape=&quot;line&quot;&gt;
            &lt;solid android:color=&quot;@android:color/transparent&quot;/&gt;
        &lt;/shape&gt;
    &lt;/item&gt;
&lt;/selector&gt;

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

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

答案1

得分: 1

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

    <RadioGroup
         android:id="@+id/radio_group"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:weightSum="3">
    
         <RadioButton
             android:id="@+id/rb_collections"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:background="@drawable/te"
             android:button="@null"
             android:text="Collections"
             android:textAlignment="center" />
    
         <RadioButton
             android:id="@+id/rb_inspections"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:button="@null"
             android:text="Inspections"
             android:textAlignment="center" />
    
         <RadioButton
             android:id="@+id/rb_enforcements"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:button="@null"
             android:text="Enforcements"
             android:textAlignment="center" />
    </RadioGroup>
    
  2. 创建一个带有底部线的背景,用于选中的按钮

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

    val rb = findViewById<RadioGroup>(R.id.radio_group)
    val rbCollections = findViewById<RadioButton>(R.id.rb_collections)
    val rbInspections = findViewById<RadioButton>(R.id.rb_inspections)
    val rbEnforcements = findViewById<RadioButton>(R.id.rb_enforcements)
    rb.setOnCheckedChangeListener { group, checkedId ->
         // 移除每个按钮的背景
         rbCollections.setBackgroundResource(0)
         rbInspections.setBackgroundResource(0)
         rbEnforcements.setBackgroundResource(0)
    
         // 设置被选中按钮的背景
         when(checkedId){
             R.id.rb_collections -> rbCollections.setBackgroundResource(R.drawable.bg)
             R.id.rb_inspections -> rbInspections.setBackgroundResource(R.drawable.bg)
             R.id.rb_enforcements -> rbEnforcements.setBackgroundResource(R.drawable.bg)
         }
    }
    
英文:
  1. Give ids to your radion group and radio buttons

    &lt;RadioGroup
         android:id=&quot;@+id/radio_group&quot;
         android:layout_width=&quot;match_parent&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:orientation=&quot;horizontal&quot;
         android:weightSum=&quot;3&quot;&gt;
    
         &lt;RadioButton
             android:id=&quot;@+id/rb_collections&quot;
             android:layout_width=&quot;wrap_content&quot;
             android:layout_height=&quot;wrap_content&quot;
             android:layout_weight=&quot;1&quot;
             android:background=&quot;@drawable/te&quot;
             android:button=&quot;@null&quot;
             android:text=&quot;Collections&quot;
             android:textAlignment=&quot;center&quot; /&gt;
    
         &lt;RadioButton
             android:id=&quot;@+id/rb_inspections&quot;
             android:layout_width=&quot;wrap_content&quot;
             android:layout_height=&quot;wrap_content&quot;
             android:layout_weight=&quot;1&quot;
             android:button=&quot;@null&quot;
             android:text=&quot;Inspections&quot;
             android:textAlignment=&quot;center&quot; /&gt;
    
         &lt;RadioButton
             android:id=&quot;@+id/rb_enforcements&quot;
             android:layout_width=&quot;wrap_content&quot;
             android:layout_height=&quot;wrap_content&quot;
             android:layout_weight=&quot;1&quot;
             android:button=&quot;@null&quot;
             android:text=&quot;Enforcements&quot;
             android:textAlignment=&quot;center&quot; /&gt;
    &lt;/RadioGroup&gt;
    
  2. Create a background with the bottom line that you need for selected button

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

    val rb = findViewById&lt;RadioGroup&gt;(R.id.radio_group)
    val rbCollections = findViewById&lt;RadioButton&gt;(R.id.rb_collections)
    val rbInspections = findViewById&lt;RadioButton&gt;(R.id.rb_inspections)
    val rbEnforcements = findViewById&lt;RadioButton&gt;(R.id.rb_enforcements)
    rb.setOnCheckedChangeListener { group, checkedId -&gt;
         // remove background of every button 
         rbCollections.setBackgroundResource(0)
         rbInspections.setBackgroundResource(0)
         rbEnforcements.setBackgroundResource(0)
    
         // set background of button which is checked
         when(checkedId){
             R.id.rb_collections -&gt; rbCollections.setBackgroundResource(R.drawable.bg)
             R.id.rb_inspections -&gt; rbInspections.setBackgroundResource(R.drawable.bg)
             R.id.rb_enforcements -&gt; rbEnforcements.setBackgroundResource(R.drawable.bg)
         }
    }
    

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:

确定