我找到了解决方案并更新了代码。希望它可以帮助别人。
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener {
TextView timer;
RadioGroup rg1;
RadioGroup rg2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
timer = (TextView) findViewById(R.id.softTimer);
rg1 = (RadioGroup) findViewById(R.id.myRadioGroup1);
rg1.setOnCheckedChangeListener(this);
rg2 = (RadioGroup) findViewById(R.id.myRadioGroup2);
rg2.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton s1 = (RadioButton) findViewById(R.id.size_small);
RadioButton s2 = (RadioButton) findViewById(R.id.size_medium);
RadioButton s3 = (RadioButton) findViewById(R.id.size_large);
RadioButton s4 = (RadioButton) findViewById(R.id.size_xlarge);
RadioButton b1 = (RadioButton) findViewById(R.id.boil_soft);
RadioButton b2 = (RadioButton) findViewById(R.id.boil_medium);
RadioButton b3 = (RadioButton) findViewById(R.id.boil_hard);
if (s1.isChecked() && b1.isChecked()){
tid = 204000;
eggTimer.setText("00:03:24");
}
else if (s1.isChecked() && b2.isChecked()) {
tid = 255000;
eggTimer.setText("00:04:15");
}
else if (s1.isChecked() && b3.isChecked()) {
tid = 341000;
eggTimer.setText("00:05:41");
}
else if (s2.isChecked() && b1.isChecked()){
tid = 239000;
eggTimer.setText("00:03:59");
}
else if (s2.isChecked() && b2.isChecked()) {
tid = 279000;
eggTimer.setText("00:04:39");
}
else if (s2.isChecked() && b3.isChecked()) {
tid = 396000;
eggTimer.setText("00:06:36");
}
else if (s3.isChecked() && b1.isChecked()){
tid = 270000;
eggTimer.setText("00:04:30");
}
else if (s3.isChecked() && b2.isChecked()) {
tid = 325000;
eggTimer.setText("00:05:25");
}
else if (s3.isChecked() && b3.isChecked()) {
tid = 485000;
eggTimer.setText("00:08:05");
}
else if (s4.isChecked() && b1.isChecked()){
tid = 336000;
eggTimer.setText("00:05:36");
}
else if (s4.isChecked() && b2.isChecked()) {
tid = 400000;
eggTimer.setText("00:06:40");
}
else if (s4.isChecked() && b3.isChecked()) {
tid = 603000;
eggTimer.setText("00:10:03");
}
final CounterClass timer = new CounterClass(tid, 1000);
tv_start_egg1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
timer.start();
}
});
tv_stop_egg1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
timer.cancel();
tid = 0;
eggTimer.setText("00:00:00");
}
});
}
}
答案 0 :(得分:0)
您可以使用此功能从广播组2中获取已检查的单选按钮ID。
int radioButtonID = rg2.getCheckedRadioButtonId();
现在相应地处理你的情况。
答案 1 :(得分:0)
容易。像这样:
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(group.getId()) {
case R.id.myRadioGroup1:
//We are in the first radio group.
switch(checkedId) {
case R.id.size_small:
timer.setText("00:04:15");
break;
case R.id.size_large:
timer.setText("00:01:15");
break;
}
break;
case R.id.myRadioGroup2:
//We are in the second radio group. Do as you need.
break;
}
}
然而,我很困惑,为什么你不要只为第二组使用int checkedId
。检查的每个单选按钮都有不同的ID。
答案 2 :(得分:0)
问题解决了。我已经更新了代码。希望它可以帮助别人。