Android:RadioGroup - 如何配置事件监听器
根据我的理解,要确定一个复选框是否被“单击”并确定它是否被选中,可以使用如下代码:
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 上?
From my understanding, to determine if a checkbox is "clicked" and find if it's checked or not, code such as the following can be used:
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");
}
}
However, I am unable to work out the logic on how to do the above for a radiogroup.
Here is the xml for my RadioGroup:
<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>
Question: Do i need to setup another listener, or will the listener already there also "register" this group?
Also, should the listener be set up on the RadioGroup or the RadioButton?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是获取选中单选按钮的方法:
要使用侦听器,您可以执行以下操作:
This is how you get the checked radiobutton:
To use the listener, you do this:
应该是这样的。
根据checkedId,您将知道哪个单选按钮已被单击,然后使用上面的代码来确定其是否已选中或未选中。这是家庭作业。 ;)
It should be something like this.
Based on the checkedId, you would know which of the radiobutton has been clicked and then use your above code to figure out if its checked or unchecked. This is homework. ;)
使用 Switch 更好:
Using Switch is better:
如果您想查看单选按钮组中选中或选择的单选按钮,请使用以下命令:
If you want to see which radio Button is checked or selected in the radio group then use the following:
我的 MainActivity.java
Activity_main.xml
SecondActivity.java
Activity_Second.xml
My MainActivity.java
activity_main.xml
secondActivity.java
activity_second.xml