Answers:
我认为更好的方法是使用RadioGroup
并设置侦听器以更改和更新View
相应的侦听器(节省2或3或4个侦听器)。
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
});
希望这个能对您有所帮助...
RadioButton rb = (RadioButton) findViewById(R.id.yourFirstRadioButton);
rb.setOnClickListener(first_radio_listener);
和
OnClickListener first_radio_listener = new OnClickListener (){
public void onClick(View v) {
//Your Implementaions...
}
};
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb=(RadioButton)findViewById(checkedId);
textViewChoice.setText("You Selected " + rb.getText());
//Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
}
});
group
在RadioButton rb=(RadioButton)group.findViewById(checkedId);
问题是关于检测单击了哪个单选按钮,这是如何获取单击哪个按钮的方法
final RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View radioButton = radio.findViewById(checkedId);
int index = radio.indexOfChild(radioButton);
// Add logic here
switch (index) {
case 0: // first button
Toast.makeText(getApplicationContext(), "Selected button number " + index, 500).show();
break;
case 1: // secondbutton
Toast.makeText(getApplicationContext(), "Selected button number " + index, 500).show();
break;
}
}
});
您还可以android:onClick="onRadioButtonClicked"
在<RadioButton/>
标记中通过XML布局添加侦听器:
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pirates"
android:onClick="onRadioButtonClicked"/>
有关详细信息,请参见Android开发者SDK-单选按钮。
以防万一有人在为接受的答案而苦苦挣扎:
有不同的OnCheckedChangeListener接口。我添加到第一个,以查看是否已更改CheckBox。
import android.widget.CompoundButton.OnCheckedChangeListener;
与
import android.widget.RadioGroup.OnCheckedChangeListener;
从Ricky添加代码片段时,我遇到了错误:
RadioGroup类型的方法setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener)不适用于参数(new CompoundButton.OnCheckedChangeListener(){})
可以用Ali的答案解决:
new RadioGroup.OnCheckedChangeListener()
由于此问题并非特定于Java,因此我想补充一下如何在Kotlin中做到这一点:
radio_group_id.setOnCheckedChangeListener({ radioGroup, optionId -> {
when (optionId) {
R.id.radio_button_1 -> {
// do something when radio button 1 is selected
}
// add more cases here to handle other buttons in the RadioGroup
}
}
})
这radio_group_id
是android:id
有关无线电组的分配。要以这种方式使用它,您将需要导入kotlinx.android.synthetic.main.your_layout_name.*
活动的Kotlin文件。另请注意,如果radioGroup
未使用lambda参数,_
则从Kotlin 1.1开始,可以将其替换为(下划线)。
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnClickListener(v -> {
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = findViewById(selectedId);
String slectedValue=radioButton.getText()
});
对于Kotlin,这里添加了lambda表达式并优化了代码。
radioGroup.setOnCheckedChangeListener { radioGroup, optionId ->
run {
when (optionId) {
R.id.radioButton1 -> {
// do something when radio button 1 is selected
}
R.id.radioButton2 -> {
// do something when radio button 2 is selected
}
// add more cases here to handle other buttons in the your RadioGroup
}
}
}
希望这会帮助你。谢谢!