Объединить 2 Радиогруппы в Android
У меня есть 2 радиогруппы. Первый с 4 кнопками, а второй с 3 кнопками.
Мне нужно решение для обновления таймера в TextView в зависимости от того, что проверяется в обеих радиогруппах. В первой радиогруппе пользователь выбирает размер, а во второй-форму. Это дает 12 комбинаций. До сих пор я только выяснил, как обновить поле TextView из 1 radiogroup. В приведенном ниже коде я показал 2 значения из первой радиогруппы (rg1). Они должны меняться в зависимости от того, что есть выбранный в радиогруппе 2 (rg2)
Я нашел решение и обновил код. Надеюсь, это поможет другим.
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");
}
});
}
}
3 ответов:
Вы можете использовать это, чтобы получить проверенный идентификатор радиокнопки из группы Радио 2.
int radioButtonID = rg2.getCheckedRadioButtonId();Теперь обрабатывайте свои условия соответственно.
Легко. Вот так:
Однако я не понимаю, почему вы не используете@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и для второй группы. Каждая переключаемая кнопка имеет свой идентификатор.
Comments