//根據(jù)ID找到RadioGroup實(shí)例
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup);
//綁定一個(gè)匿名監(jiān)聽器
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
//獲取變更后的選中項(xiàng)的ID
int radioButtonId = arg0.getCheckedRadioButtonId();
switch(radioButtonId){
case R.id.radiobtn1:
//todo what you want
break;
case R.id.radiobtn2:
//todo what you want
break;
}
}
});
xml文件中RadioButton必須放在 RadioGroup中装获。