Working with Radiobutton and Radiogroup

Asked

Viewed 13,850 times

2

I’m trying to make a screen where the user would have to choose between the options on <RadioButton and if he chooses the right one counts for the right one and otherwise for the wrong one, having finished, after clicking on "Reply" the same is in the print and the piece of code below:

RadioBtn

    public void onClickView(View rdio){
    int qnt_certo = 0, qnt_errado = 0;
      RadioGroup rd_group = (RadioGroup) findViewById(R.id.perguntas);
        switch (rd_group.getCheckedRadioButtonId()){
          case R.id.rd_btn1:
              qnt_errado = qnt_errado+1;
              break;
          case R.id.rd_btn2:
              qnt_errado = qnt_errado+1;
              break;
          case R.id.rd_btn3:
              qnt_certo = qnt_certo +1;
              break;
          case R.id.rd_btn4:
              qnt_errado = qnt_errado+1;
              break;
        }
    }

But the part where I found it difficult was to manipulate the information, I’ll explain:

I want after the user choose and click on Answer back that <RadioGroup would be blocked, so he could not change his choice and his "vote" accounted for. My difficulties:

1- Know when and what the user chose.

2- Block the <RadioGroupwhen he Answer back

3- Send information to another activity for it to have a percentage of right and wrong

4- After choosing the <RadioButton change color (if right or wrong)

1 answer

4


You can use the method setOnCheckedChangeListener of Radiogroup as follows:

final RadioGroup group = (RadioGroup) findViewById(R.id.group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton button = (RadioButton) group.findViewById(checkedId);
            String resposta = button.getText().toString();
        }
    });

With this you have the Radiobutton selected, so you can change the color, get the text, change the text or any other operation of Radiobutton.

To disable Radiogroup you need to disable all elements within it. This can be done with the following function:

private void disableAllOptions(RadioGroup group) {
    for (int i = 0; i < group.getChildCount(); i++) {
        group.getChildAt(i).setEnabled(false);
    }
}

This function can be called within the Listener, as follows:

final RadioGroup group = (RadioGroup) findViewById(R.id.group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton button = (RadioButton) group.findViewById(checkedId);
            String resposta = button.getText().toString();

            disableAllOptions(group);
        }
    });

To send the values to another Activity you must use the methods putExtra in the Intent you use to start the other Activity.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.