51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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

英文:

how to Underline the selected radio button in android?

问题 {#heading}

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;

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

            &amp;lt;RadioButton

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

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

        &amp;lt;/RadioGroup&amp;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

  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;
    
         &amp;lt;RadioButton
             android:id=&amp;quot;@+id/rb_collections&amp;quot;
             android:layout_width=&amp;quot;wrap_content&amp;quot;
             android:layout_height=&amp;quot;wrap_content&amp;quot;
             android:layout_weight=&amp;quot;1&amp;quot;
             android:background=&amp;quot;@drawable/te&amp;quot;
             android:button=&amp;quot;@null&amp;quot;
             android:text=&amp;quot;Collections&amp;quot;
             android:textAlignment=&amp;quot;center&amp;quot; /&amp;gt;
    
         &amp;lt;RadioButton
             android:id=&amp;quot;@+id/rb_inspections&amp;quot;
             android:layout_width=&amp;quot;wrap_content&amp;quot;
             android:layout_height=&amp;quot;wrap_content&amp;quot;
             android:layout_weight=&amp;quot;1&amp;quot;
             android:button=&amp;quot;@null&amp;quot;
             android:text=&amp;quot;Inspections&amp;quot;
             android:textAlignment=&amp;quot;center&amp;quot; /&amp;gt;
    
         &amp;lt;RadioButton
             android:id=&amp;quot;@+id/rb_enforcements&amp;quot;
             android:layout_width=&amp;quot;wrap_content&amp;quot;
             android:layout_height=&amp;quot;wrap_content&amp;quot;
             android:layout_weight=&amp;quot;1&amp;quot;
             android:button=&amp;quot;@null&amp;quot;
             android:text=&amp;quot;Enforcements&amp;quot;
             android:textAlignment=&amp;quot;center&amp;quot; /&amp;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 -&amp;gt; rbCollections.setBackgroundResource(R.drawable.bg)
             R.id.rb_inspections -&amp;gt; rbInspections.setBackgroundResource(R.drawable.bg)
             R.id.rb_enforcements -&amp;gt; rbEnforcements.setBackgroundResource(R.drawable.bg)
         }
    
    
    
    `}
    `
    

赞(0)
未经允许不得转载:工具盒子 » 在Android中如何给选中的单选按钮添加下划线?