lundi 3 avril 2017

getting checkboxes to work like radio group

i have a inspection app that has questions that have answers that are below avg, average, and above avg. I'm using checkboxes instead of radio group because i could not get the radio buttons to work like i wanted.

the score changes as soon as something is clicked. i can't figure out to get it to remove the added score if you change your answer, it just keeps adding more to the score.

    below32 = (CheckBox) findViewById(R.id.below32);
    below32.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (below32.isChecked()) {
                sum += 1;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
                avg32.setChecked(false);
                above32.setChecked(false);
            }
            else {
                sum -= 1;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
            }
        }
    });
    avg32 = (CheckBox) findViewById(R.id.avg32);
    avg32.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (avg32.isChecked()) {
                sum += 2;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
                below32.setChecked(false);
                above32.setChecked(false);
            }
            else {
                sum -= 2;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
            }
        }
    });
    above32 = (CheckBox) findViewById(R.id.above32);
    above32.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (above32.isChecked()) {
                sum += 3;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
                below32.setChecked(false);
                avg32.setChecked(false);
            }
            else {
                sum -= 3;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
            }
        }
    });

what do i need to change to make this work?




Aucun commentaire:

Enregistrer un commentaire