重置后无法选择先前选择的单选按钮

时间:2012-11-24 13:24:51

标签: android radio-button unchecked

我有一组三个RadioGroup,每个组由三个RadioButtons组成。还有一个重置按钮,用于将所有组中的所有RadioButton设置为false(setChecked(false))。

重置工作正常(所有RadioButtons都未选中),但重置后,在每个RadioGroup中,只有那两个按钮可以被检查,在点击重置按钮的时刻没有检查。只有这样,才能再次检查组中的所有三个按钮。

我尝试在重置期间添加setFocusable(true)和setClickable(true),但这没有什么区别。那么焦点或可点击性还有什么可能导致这种情况以及如何解决?

我定位Android API 7。

这是完整的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;
    }

}

2 个答案:

答案 0 :(得分:9)

在这里找到了:

android radiobutton check after clearing check issue

对于每个单独的Button,应该使用radioGroup.clearCheck()而不是radioButton.setChecked(false)。

答案 1 :(得分:0)

RadioButton组强制执行此规则:任何时候只选择一个选项。你不应该取消选中或检查多个。 CheckBox专为此类用途而设计。