【发布时间】:2011-10-10 11:50:11
【问题描述】:
据我了解,要判断一个复选框是否被“点击”并判断它是否被选中,可以使用如下代码:
cb=(CheckBox)findViewById(R.id.chkBox1);
cb.setOnCheckedChangeListener(this);
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
cb.setText("This checkbox is: checked");
}
else {
cb.setText("This checkbox is: unchecked");
}
}
但是,我无法弄清楚如何为无线电组执行上述操作的逻辑。
这是我的 RadioGroup 的 xml:
<RadioGroup android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio1" android:checked="true"
android:text="RadioButton1">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio2" android:text="RadioButton2" android:checked="true">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio3" android:text="RadioButton3">
</RadioButton>
</RadioGroup>
问题:我是否需要设置另一个侦听器,或者已经存在的侦听器也会“注册”这个组?
另外,监听器应该设置在 RadioGroup 还是 RadioButton 上?
【问题讨论】:
标签: android listener radio-group android-radiogroup android-radiobutton