Question

I have a set of three RadioGroups each consisting of three RadioButtons. And a reset Button that sets all RadioButtons in all Groups to false (setChecked(false)).

Reset works fine (all RadioButtons are unchecked) except that after a reset, in each RadioGroup, only those two buttons can be checked that where not checked at the moment that the reset buton was clicked. Only after doing that all three buttons in the group can be checked again just like before.

I tried to add setFocusable(true) and setClickable(true) during reset but that doesn't make a difference. What else then focus or clickability may cause this and how to solve?

I target Android API 7.

Here's the complete MainActivity.java:

    package nl.ch.simplescore;

import nl.ch.simplescore.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button scoretoevoegenButton; 
    RadioButton radio11RadioButton;
    RadioButton radio12RadioButton;
    RadioButton radio13RadioButton;
    RadioButton radio21RadioButton;
    RadioButton radio22RadioButton;
    RadioButton radio23RadioButton;
    RadioButton radio31RadioButton;
    RadioButton radio32RadioButton;
    RadioButton radio33RadioButton;

    TextView textView15;
    TextView textView16;
    TextView textView17;
    TextView textView18;
    TextView textView19;
    TextView textView20;
    TextView textView21;
    TextView textView22;
    TextView textView23;
    TextView textView24;
    TextView textView25;
    TextView textView26;


    int speler1score = 0;
    int speler2score = 0;
    int speler3score = 0;

    int speler1score123 = 0;
    int speler1score12 = 0;
    int speler1score13 = 0;
    int speler1score23 = 0;
    int speler2score123 = 0;
    int speler2score12 = 0;
    int speler2score13 = 0;
    int speler2score23 = 0;
    int speler3score123 = 0;
    int speler3score12 = 0;
    int speler3score13 = 0;
    int speler3score23 = 0;

    int speler1sumscore123 = 0;
    int speler1sumscore12 = 0;
    int speler1sumscore13 = 0;
    int speler1sumscore23 = 0;
    int speler2sumscore123 = 0;
    int speler2sumscore12 = 0;
    int speler2sumscore13 = 0;
    int speler2sumscore23 = 0;
    int speler3sumscore123 = 0;
    int speler3sumscore12 = 0;
    int speler3sumscore13 = 0;
    int speler3sumscore23 = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scoretoevoegenButton = (Button) this.findViewById(R.id.button1);
        radio11RadioButton = (RadioButton) this.findViewById(R.id.radio11);
        radio12RadioButton = (RadioButton) this.findViewById(R.id.radio12);
        radio13RadioButton = (RadioButton) this.findViewById(R.id.radio13);
        radio21RadioButton = (RadioButton) this.findViewById(R.id.radio21);
        radio22RadioButton = (RadioButton) this.findViewById(R.id.radio22);
        radio23RadioButton = (RadioButton) this.findViewById(R.id.radio23);
        radio31RadioButton = (RadioButton) this.findViewById(R.id.radio31);
        radio32RadioButton = (RadioButton) this.findViewById(R.id.radio32);
        radio33RadioButton = (RadioButton) this.findViewById(R.id.radio33);

        textView15 = (TextView) this.findViewById(R.id.textView15);
        textView16 = (TextView) this.findViewById(R.id.textView16);
        textView17 = (TextView) this.findViewById(R.id.textView17);
        textView18 = (TextView) this.findViewById(R.id.textView18);
        textView19 = (TextView) this.findViewById(R.id.textView19);
        textView20 = (TextView) this.findViewById(R.id.textView20);
        textView21 = (TextView) this.findViewById(R.id.textView21);
        textView22 = (TextView) this.findViewById(R.id.textView22);
        textView23 = (TextView) this.findViewById(R.id.textView23);
        textView24 = (TextView) this.findViewById(R.id.textView24);
        textView25 = (TextView) this.findViewById(R.id.textView25);
        textView26 = (TextView) this.findViewById(R.id.textView26);

            }

    public void radio11ClickHandler(View v) {
        speler1score = 3;
    }

    public void radio12ClickHandler(View v) {
        speler1score = 2;
    }

    public void radio13ClickHandler(View v) {
        speler1score = 1;
    }

    public void radio21ClickHandler(View v) {
        speler2score = 3;
    }

    public void radio22ClickHandler(View v) {
        speler2score = 2;
    }

    public void radio23ClickHandler(View v) {
        speler2score = 1;
    }

    public void radio31ClickHandler(View v) {
        speler3score = 3;
    }

    public void radio32ClickHandler(View v) {
        speler3score = 2;
    }

    public void radio33ClickHandler(View v) {
        speler3score = 1;
    }

    public void scoretoevoegenClickHandler(View v) {
        scoreTonen();
    }

    public void resetClickHandler(View v) {
        speler1score = 0;
        speler2score = 0;
        speler3score = 0;

        radio11RadioButton.setChecked(false);
        radio12RadioButton.setChecked(false);
        radio13RadioButton.setChecked(false);
        radio21RadioButton.setChecked(false);
        radio22RadioButton.setChecked(false);
        radio23RadioButton.setChecked(false);
        radio31RadioButton.setChecked(false);
        radio32RadioButton.setChecked(false);
        radio33RadioButton.setChecked(false);

        textView15.setText(null);
        textView16.setText(null);
        textView17.setText(null);
        textView18.setText(null);
        textView19.setText(null);
        textView20.setText(null);
        textView21.setText(null);
        textView22.setText(null);
        textView23.setText(null);
        textView24.setText(null);
        textView25.setText(null);
        textView26.setText(null);

    }

    public void scoreTonen() {
        int kolom; 

        kolom = 0;
        if (speler1score!=0) {
            if (speler2score!=0 && speler3score!=0) {
                kolom = 123;
            }
            if (speler2score!=0 && speler3score==0) {
                kolom = 12;
            }
            if (speler2score==0 && speler3score!=0) {
                kolom = 13;
            }
            switch (kolom) {
            case 123: 
                textView15.setText(Integer.toString(speler1score));
                break;
            case 12:    
                textView16.setText(Integer.toString(speler1score));
                break;
            case 13:
                textView17.setText(Integer.toString(speler1score));
                break;
            default: // niets doen  
            }
        }

        kolom = 0;
        if (speler2score!=0) {
            if (speler1score!=0 && speler3score!=0) {
                kolom = 123;
            }
            if (speler1score!=0 && speler3score==0) {
                kolom = 12;
            }
            if (speler1score==0 && speler3score!=0) {
                kolom = 23;
            }
            switch (kolom) {
            case 123: 
                textView19.setText(Integer.toString(speler2score));
                break;
            case 12:    
                textView20.setText(Integer.toString(speler2score));
                break;
            case 23:
                textView22.setText(Integer.toString(speler2score));
                break;
            default: // niets doen  
            }
        }

        kolom = 0;
        if (speler3score!=0) {
            if (speler1score!=0 && speler2score!=0) {
                kolom = 123;
            }
            if (speler1score!=0 && speler2score==0) {
                kolom = 13;
            }
            if (speler1score==0 && speler2score!=0) {
                kolom = 23;
            }
            switch (kolom) {
            case 123: 
                textView23.setText(Integer.toString(speler3score));
                break;
            case 13:    
                textView25.setText(Integer.toString(speler3score));
                break;
            case 23:
                textView26.setText(Integer.toString(speler3score));
                break;
            default: // niets doen  
            }
        }
    }

    public void sumScoreTonen() {
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
Was it helpful?

Solution

found it here:

android radiobutton check after clearing check issue

Should have used radioGroup.clearCheck() instead of radioButton.setChecked(false) for each seperate Button.

OTHER TIPS

RadioButton group enforces this rule: only one option selected at any time. You should't uncheck all or check more than one. CheckBox is designed for such use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top