Android:获取所选单选按钮的Radio组的ID

时间:2017-07-15 21:04:16

标签: android

我有2 Radio Groups。每个人有2 Radio buttons。如何获得所选id的{​​{1}} radio group

1 个答案:

答案 0 :(得分:0)

RadioGroup添加到RadioGroup,然后从方法ID获取所选的onCheckedChanged() int selectedRadioGroupId = 0; // Group 1 radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { selectedRadioGroupId = radioGroup.getId(); switch (checkedId) { case R.id.button1: // Do something Toast.makeText(getApplicationContext(), radioButton1.getText() + " clicked", Toast.LENGTH_SHORT).show(); break; case R.id.button2: // Do something Toast.makeText(getApplicationContext(), radioButton2.getText() + " clicked", Toast.LENGTH_SHORT).show(); break; } } }); // Group 2 radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { selectedRadioGroupId = radioGroup.getId(); switch (checkedId) { case R.id.button3: // Do something Toast.makeText(getApplicationContext(), radioButton3.getText() + " clicked", Toast.LENGTH_SHORT).show(); break; case R.id.button4: // Do something Toast.makeText(getApplicationContext(), radioButton4.getText() + " clicked", Toast.LENGTH_SHORT).show(); break; } } });

试试这个:

let snapshot: DataSnapshot = self.dataSource.items[indexPath.row]