2012-11-24 50 views
1

我有一組三個RadioGroups,每個RadioGroups都由三個RadioButton組成。還有一個重置按鈕,將所有組中的所有RadioButton都設置爲false(setChecked(false))。復位後無法選擇先前選擇的RadioButton

重置工作正常(所有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; 
    } 

} 

回答

0

RadioButton組強制執行這個規則:在任何時候只選擇一個選項。您不應該取消全部選中或選中多個選項。 CheckBox是專爲這種使用而設計的。

+0

這些按鈕實際上最初是未經檢查的(除非您選擇將其中一個設置爲選中狀態),所以我不希望無法以某種方式返回到初始狀態。 – user1837293