Working with RadioButton and RadioGroup

1

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

publicvoidonClickView(Viewrdio){intqnt_certo=0,qnt_errado=0;RadioGrouprd_group=(RadioGroup)findViewById(R.id.perguntas);switch(rd_group.getCheckedRadioButtonId()){caseR.id.rd_btn1:qnt_errado=qnt_errado+1;break;caseR.id.rd_btn2:qnt_errado=qnt_errado+1;break;caseR.id.rd_btn3:qnt_certo=qnt_certo+1;break;caseR.id.rd_btn4:qnt_errado=qnt_errado+1;break;}}

ButthepartwhereIfounddifficultywastomanipulatetheinformation,Iwillexplain:

IwantthataftertheuserchoosesandclicksReplythat<RadioGroupwouldbeblocked,sohecouldnotchangehischoiceandhis"vote" posted. My difficulties:

1- Know when and what the user chose.

2 - Block <RadioGroup when it Reply

3- Send the information to another activity so it has a percentage of hits and errors

4 - After choosing% change color (if right or wrong)

    
asked by anonymous 19.04.2016 / 05:19

1 answer

3

You can use RadioGroup's setOnCheckedChangeListener method 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 RadioButton operation.

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 inside 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 putExtra methods in the Intent that you use to start the other Activity.

    
19.04.2016 / 14:57