【问题标题】:Ensure one of the RadioButton in a RadioGroup is selected in Android确保在 Android 中选择 RadioGroup 中的 RadioButton 之一
【发布时间】:2013-07-23 20:00:13
【问题描述】:

如果我有以下电台组:

<RadioGroup
    android:id="@+id/rgType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="6" >

    <RadioButton
        android:id="@+id/rbBrides"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Bridge" />

    <RadioButton
        android:id="@+id/gbTunnel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tunnel" />

    <RadioButton
        android:id="@+id/rbHighway"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Highway" />
</RadioGroup>

我想做以下检查:

If (rgType is selected) {
system.out.println("ok");
}
if (rgType is void/null/not selected) {
system.out.println("choose at least one selection");
}

我可以使用用于单个单选按钮的isChecked(),如下所示?

if (rgType.isChecked()) {

}
else {
}

【问题讨论】:

    标签: android radio-button radio-group


    【解决方案1】:

    您可以使用getCheckedRadioButtonId 并查看您的RadioButtons 之一是否被选中。如果没有,我不确定它会返回什么,但可能是null 或更可能是-1。无论哪种方式,运行它,看看它返回什么。

    如果没有检查,我相信它确实会返回 -1,所以请尝试

      if (rgType == -1)
      {
          system.out.println("choose at least one selection");        
      }
      else
      {
           system.out.println("ok");  
      }
    

    RadioGroup

    【讨论】:

    • 我能得到一个 indexOf 吗?
    • 索引是什么?如果它们在一个数组中,那么我想你可以。你到底想要什么?查看我发布的链接,它为您提供了可用的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2020-11-21
    • 2014-04-04
    • 2013-08-03
    • 2016-09-15
    相关资源
    最近更新 更多