2012-06-15 50 views
0

在我的編碼m有三個按鈕。我得到的選擇按鈕是id並用它來比較。但是在點擊按鈕之前,它會自動顯示默認選定按鈕的ID。單選按鈕沒有顯示正確的值

rdgp.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       counter++; 
       if (counter > 5 || counter >= anarray.size()) { 
        Toast.makeText(testActivity.this, "" + score, 2000).show(); 
        Intent intent = new Intent(testActivity.this, Badge.class); 
        intent.putExtra("Score", score); 
        startActivity(intent); 
        return; 
       } 

       rb1.setText(anarray.get(counter)); 
       rb2.setText(an1array.get(counter)); 
       rb3.setText(an2array.get(counter)); 
       rb4.setText(an3array.get(counter)); 
       rb5.setText(an4array.get(counter)); 
       int ans = 0; 
       if (checkedId == R.id.radio0) { 
        ans = 1; 
       } else if (checkedId == R.id.radio1) { 
        ans = 2; 
       } else if (checkedId == R.id.radio2) { 
        ans = 3; 
       } else if (checkedId == R.id.radio3) { 
        ans = 4; 
       } 

       Toast.makeText(testActivity.this, "" + ans, 2000).show(); 
       if (ans == anarray.get(counter)) 
        ; 
       { 
        score = score + 1; 
       } 
      } 
     }); 
+0

它顯示的默認值是 – Khan

回答

0

得到檢查radiobutton使用group.getCheckedRadioButtonId() .Returns的id該組中所選擇的單選按鈕的標識符。在空選擇時,返回值爲-1。

0

在您的XML中爲所有單選按鈕添加以下行。

android:checked="false" 
+0

它正在處理第一組數據。當onclick移動到下一組數據時,第一組數據中的點擊按鈕顯示,並且該值被選擇用於比較。 – selvp

0

如果您收到來自單選按鈕默認值,那麼在你使用過Android我們的佈局文件天氣檢查:檢查=「真」。如果這是那麼國家將其更改爲false。