2017-08-31 119 views
-1

有時當我想在單選按鈕中選擇選項時,單選按鈕出現問題,我可以選擇兩個收音機但是有時候它是正確的,因爲它必須工作。單選按鈕在android中出錯

這裏有兩個樣品中我的應用程序:sample1sample2

這裏是我的代碼:

span.replace(0,0,getText(R.string.ForeSingleChoice)); 
        new DynamicViews().makeNormalContent(getApplicationContext(),span,linearLayout,16,16,16,16,16.0f); 
        span.clear(); 

        radioGroup = new DynamicViews().makeRadioGroup(getApplicationContext(),linearLayout); 
        new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop1_2),-1); 
        new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop2_2),5); 
        new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop3_2),-1); 
        new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop4_2),-1); 

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
         @Override 
         public void onCheckedChanged(RadioGroup group, int checkedId) { 
          switch (checkedId) 
          { 
           case 5: goToNextLevel= true ; 
            break; 
           default: goToNextLevel = false; 
            break; 
          } 
         } 
        }); 

DynamicViews makeRadioButton:

public RadioButton makeRadioButton(Context context, RadioGroup radioGroup, String text, int Id) { 
    RadioButton radioButton = new RadioButton(context); 
    LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    radioButton.setBackgroundResource(R.drawable.backgroundbetweenradios); 
    radioButton.setLayoutParams(Params); 
    radioButton.setPadding(dpToPx(8,context) , dpToPx(8,context) , dpToPx(8,context) , dpToPx(8,context)); 
    radioButton.setId(Id); 
    radioButton.setText(text); 
    radioButton.setTextColor(Color.BLACK); 
    radioGroup.addView(radioButton); 

    return radioButton; 
} 

DynamicViews makeRadioGroup:

public RadioGroup makeRadioGroup(Context context, LinearLayout linearLayout) { 
    RadioGroup radioGroup = new RadioGroup(context); 
    LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    radioGroup.setLayoutParams(Params); 
    radioGroup.setOrientation(LinearLayout.VERTICAL); 

    CardView cardView = new CardView(context); 
    cardView.setCardBackgroundColor(Color.WHITE); 
    cardView.setCardElevation(8.0f); 
    Params.setMargins(dpToPx(16,context) , dpToPx(16,context) , dpToPx(16,context) , dpToPx(16,context)); 
    cardView.setLayoutParams(Params); 
    cardView.addView(radioGroup); 
    linearLayout.addView(cardView); 

    return radioGroup; 
} 

我的應用程序由很多單選按鈕組成,這是我製作這些單選按鈕的方法,有什麼問題?這是一個錯誤?

+0

我建議你使用'radioGroup'而不是使用多個'radioButtons'! – sam

+0

@sam我做了一個radiogroup,並添加了4個單選按鈕 –

回答

0

您是否嘗試過給每個單選按鈕的唯一ID?

new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop1_2),1); 
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop2_2),5); 
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop3_2),2); 
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop4_2),3); 
+0

將-1分配給ID將會超出可用範圍。所以可以假設在默認情況下。這不正確嗎? –

+0

我改變了他們的正數,似乎有效! –

+0

這是因爲RadioGroup基於id區分它的RadioButton,因此當少數RadioButton具有相同的id時,RadioGroup可以將它們視爲一個按鈕。 –

0

上添加這個您onCreate()

CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked){ 
        rbtn1.setChecked(yesrbtn == buttonView); 
        rbtn2.setChecked(norbtn == buttonView); 
        .... 

       } 

      } 

     }; 

    rbtn1.setOnCheckedChangeListener(listener); 
    rbtn2.setOnCheckedChangeListener(listener); 

這樣做是確保單選按鈕不允許有2個以上的選擇

+0

我的所有收音機都是本地對象,如何在onCreate中引用它們(超出範圍)? –

+0

調用單選按鈕的編號 – Orvenito