RadioGroup checkedButton属性

Rub*_*ben 14 xml android radio-button

我试图在Android中构建RadioGroup,默认情况下检查一个RadioButton.我想知道这是否可以通过XML而不是以编程方式完成.

以下代码片段似乎不起作用,因为我收到错误:

error: Error: No resource found that matches the given name (at 'checkedButton' with value '@id/rdb_positive')
Run Code Online (Sandbox Code Playgroud)

代码是:

<RadioGroup
    style="@style/FormInputField"
    android:orientation="vertical"
    android:checkedButton="@id/rdb_positive"> <!-- Error on this line -->
    <RadioButton
        android:id="@+id/rdb_positive"
        android:text="@string/answer_positive" />
    <RadioButton
        android:id="@+id/rdb_negative"
        android:text="@string/answer_negative" />
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)

它确实在某种程度上有意义,因为RadioButton的id是在RadioGroup中的属性设置之后定义的,但后来我想知道为什么有这样的属性可用.

Sam*_*iya 25

使用android:checkedButton="@+id/rdb_positive",我想你添加+标志然后它的作品

  • 我认为+叹息用于定义一个新的id,我很惊讶它的工作原理:-)谢谢. (8认同)
  • @LalitPoptani我知道`+`用于新的id和bug.but它的解决方案是分配`+` (3认同)
  • 当你在`RadioGroup`中使用`+`时,将创建id`rcdb_positive`.所以你不需要在`RadioButton`中再次使用`+`.所以在RadioButton中使用`android:id ="@ id/rdb_positive"`. (3认同)