我的活动中遇到单选按钮问题。我在一个radiogroup中添加了两个单选按钮。
<RadioGroup
android:layout_width="270dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<RadioButton
android:id="@+id/site_automation_yes"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="YES"
android:button="@drawable/apptheme_btn_radio_holo_light"
android:textSize="18dp"/>
<RadioButton
android:id="@+id/site_automation_no"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="NO"
android:button="@drawable/apptheme_btn_radio_holo_light"
android:textSize="18dp"/>
</RadioGroup>
当我检查site_automation_yes radioButton时,isChecked()仍然返回false。 此外,在第一次创建活动时,我无法检查“site_automation_no”单选按钮。但是当我首先检查site_automation_yes时,我可以检查并取消选中两个单选按钮。但是对于site_automation_yes,isChecked总是返回false,而不管它是否被选中。有人能告诉我如何解决这个问题。 提前谢谢。
答案 0 :(得分:1)
如果你想要检查收音机那么你必须遵循这个: 1.给无线电组织ID 2.使用以下示例:
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
这里,radioSexGroup是一个RadioGroup id。通过getCheckedRadioButtonId()获取选中的单选按钮;然后只需获取已选中单选按钮的名称。