【问题标题】:How to default check RadioButton in RadioGroup without Id?如何在没有 ID 的情况下默认检查 RadioGroup 中的 RadioButton?
【发布时间】:2019-08-29 17:58:55
【问题描述】:

所以我在 RadioGroup 中有一个 RadioButton,应该在不使用 Id 的情况下对其进行检查。

这是我的 XML 代码:

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 2" />
</RadioGroup>

这里我检查了选中的 RadioButton:

RadioGroup radioGroup = findViewById(R.id.radioGroup);
int radioButtonId = radioGroup.getCheckedRadioButtonId();

switch (radioButtonId) {
    case 1:
        doSomething();
        break;
    case 2:
        doSomething();
        break;
}

我试过了:

1) 添加属性 android:android:checkedButton 到 RadioGroup。

<RadioGroup
    android:id="@+id/radioGroup"
    android:checkedButton="@id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 2" />
</RadioGroup>

doSometheing() 未执行,因为 radioButtonId 不是索引,而是 Id。

2) 然后我在 RadioButton 中添加了一个属性 android:checked as "true"。

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="RadioButton 1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 2" />
</RadioGroup>

doSometheing() 已执行,但现在可以同时检查两个 RadioButton。无法取消选中第一个 RadioButton。 radioButtonId 保持不变 == 1。

希望你能告诉我正确的方法。

谢谢。祝你有美好的一天。

【问题讨论】:

  • 为什么你不想为radio button 提供id?每个组件都应该有一个id
  • @AbuNoman,很可能,这是正确的方法。但是当 ID 很多时,我害怕在 ID 中感到困惑。我更喜欢索引。如果我错了,请告诉我。

标签: java android android-studio


【解决方案1】:

你可以得到checkedRadioButton的索引如下:

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);

switch (idx) {
    case 1:
        doSomething();
        break;
    case 2:
        doSomething();
        break;
}

注意:不要忘记为radio buttons 分配id

【讨论】:

    【解决方案2】:

    使用 findviewbyid:

    radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1);
    radiobutton1.setChecked(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2019-10-03
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      相关资源
      最近更新 更多