RadioGroup checkedButton property

前端 未结 4 1738
盖世英雄少女心
盖世英雄少女心 2021-02-13 11:49

I am trying to build RadioGroup in Android with one RadioButton checked by default. I\'m wondering if this is possible to do through XML, rather than programmatically.

T

相关标签:
4条回答
  • 2021-02-13 12:04
    <RadioGroup
        style="@style/FormInputField"
        android:orientation="vertical"> 
        <RadioButton
            android:id="@+id/rdb_positive"
            android:text="@string/answer_positive"
            android:checked="true"/>  
        <RadioButton
            android:id="@+id/rdb_negative"
            android:text="@string/answer_negative" />
    </RadioGroup>
    

    Add android:checked="true" to the radiobutton that you want to make as default

    0 讨论(0)
  • 2021-02-13 12:08

    You can get rid of that error by declaring id rdb_positive inside ids.xml and then referencing the id from both RadioGroup and RadioButton elements.

    <RadioGroup
        style="@style/FormInputField"
        android:orientation="vertical"
        android:checkedButton="@id/rdb_positive"> <!-- REFERENCE TO ids.xml -->
        <RadioButton
            android:id="@id/rdb_positive" 
            android:text="@string/answer_positive" /> <!-- REFERENCE TO ids.xml -->
        <RadioButton
            android:id="@+id/rdb_negative"
            android:text="@string/answer_negative" />
    </RadioGroup>
    

    ids.xml:

    <resources>
        <item type="id" name="rdb_positive" />
    </resources>
    
    0 讨论(0)
  • 2021-02-13 12:13

    try this......

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <RadioButton
            android:id="@+id/rdb_positive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="answer_positive" />
    
        <RadioButton
            android:id="@+id/rdb_negative"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="answer_negative" />
    </RadioGroup>
    
    0 讨论(0)
  • 2021-02-13 12:21

    Use android:checkedButton="@+id/rdb_positive" ,i think you add + sign then its works

    0 讨论(0)
提交回复
热议问题