2012-02-07 12 views
104

साथ RadioGroup में डिफ़ॉल्ट के रूप में जाँच की गतिशीलरेडियो बटन स्थापित करने के लिए कैसे एंड्रॉयड

RadioGroup radioGroup = new RadioGroup(context); 
        RadioButton radioBtn1 = new RadioButton(context); 
        RadioButton radioBtn2 = new RadioButton(context); 
        RadioButton radioBtn3 = new RadioButton(context); 

        radioBtn1.setText("Less"); 
        radioBtn2.setText("Normal"); 
        radioBtn3.setText("More"); 

        radioBtn2.setChecked(true); 

        radioGroup.addView(radioBtn1); 
        radioGroup.addView(radioBtn2); 
        radioGroup.addView(radioBtn3); 

यहाँ कदम radioBtn2.setChecked(true); का कारण बनता है radioBtn2 हमेशा जाँच की निम्नलिखित के रूप में मैं RadioGroup और RadioButton बनाया है। इसका मतलब है कि मैं अन्य दो रेडियो बटन (radioBtn1, radioBtn3) की जांच करके radioBtn2 अनचेक नहीं कर सकता हूं। मैं इसे बनाना चाहता हूं RadioGroup एक समय में केवल एक रेडियो बटन देख सकता है (अब यह एक समय में दो रेडियोधटन की जांच कर सकता है)। इस समस्या का समाधान किस प्रकार से किया जा सकता है?

+0

में android:checked = "true" जोड़े आप गतिशील रूप से की जरूरत है ??? –

उत्तर

167

आप इस तरह RadioGroup में radiobutton की जांच होनी चाहिए:

radiogroup.check(IdOfYourButton)

बेशक

आप पहली बार अपने radiobuttons को

संपादित एक आईडी सेट करने के लिए: मैं भूल गया, radioButton.getId() रूप में अच्छी तरह से काम करता है, thx रमेश

+32

या यदि आप अपने 'रेडियोबटन' को आईडी नहीं करना चाहते हैं और आप इंडेक्स को जानते हैं तो आप ** ((रेडियोबटन) रेडियो ग्रुप.getChildAt (INDEX)) का उपयोग कर सकते हैं। सेट चेक (सत्य); ** –

+16

आपको ऐसा नहीं करना चाहिए । इससे इस प्रश्न में वर्णित "सेट चेक" समस्या का कारण बन जाएगा। निस्संदेह आपके बटन के आईडी के बिना ऐसा करने के तरीके हैं, लेकिन कृपया 'setChecked() ' का उपयोग करके नहीं, एक तरीका' radiogroup.check (((रेडियोबटन) रेडियो समूह होगा।getChildAt (INDEX))। getId()) 'या – Sprigg

+0

जैसे कुछ रेडियो बटन समूह _before_ कॉलिंग group.check (button.getId()) – tom

18
RadioGroup radioGroup = new RadioGroup(WvActivity.this); 
    RadioButton radioBtn1 = new RadioButton(this); 
    RadioButton radioBtn2 = new RadioButton(this); 
    RadioButton radioBtn3 = new RadioButton(this); 

    radioBtn1.setText("Less"); 
    radioBtn2.setText("Normal"); 
    radioBtn3.setText("More"); 


    radioGroup.addView(radioBtn1); 
    radioGroup.addView(radioBtn2); 
    radioGroup.addView(radioBtn3); 

    radioGroup.check(radioBtn2.getId()); 
4

मेरे सहयोगी के कोड में एक ही समस्या थी। ऐसा लगता है क्योंकि आपका रेडियो समूह आपके रेडियो बटन के साथ ठीक तरह से सेट नहीं है। यही कारण है कि आप रेडियो बटन का चयन कर सकते हैं। मैंने कई चीजों की कोशिश की, अंत में मैंने एक चाल की जो वास्तव में गलत है, लेकिन ठीक काम करता है।

for (int i = 0 ; i < myCount ; i++) 
{ 
    if (i != k) 
    { 
     System.out.println ("i = " + i); 
     radio1[i].setChecked(false); 
    } 
} 

यहां मैंने लूप के लिए एक सेट किया है, जो उपलब्ध रेडियो बटनों की जांच करता है और नए क्लिक किए गए एक को छोड़कर हर किसी का चयन रद्द करता है। कोशिश करो।

6
RadioGroup radioGroup = new RadioGroup(context); 
RadioButton radioBtn1 = new RadioButton(context); 
RadioButton radioBtn2 = new RadioButton(context); 
RadioButton radioBtn3 = new RadioButton(context); 

radioBtn1.setText("Less"); 
radioBtn2.setText("Normal"); 
radioBtn3.setText("More"); 

radioGroup.addView(radioBtn1); 
radioGroup.addView(radioBtn2); 
radioGroup.addView(radioBtn3); 
radioBtn2.setChecked(true); 
+0

रेडियो समूह में सभी रेडियो बटन जोड़े जाने के बाद 'सेट चेक()' का उपयोग करना महत्वपूर्ण बात है। फिर, 'RadioGroup.check (radioBtn2.getId())' निरंतर नहीं है –

93

एक्सएमएल के लिए मामले में विशेषता इसकी android:checkedButton जो RadioButton की id लेता जांच की जानी।

<RadioGroup 
... 
... 
android:checkedButton="@+id/IdOfTheRadioButtonInsideThatTobeChecked" 
... >....</RadioGroup> 
34

एक्सएमएल फ़ाइल में अपने RadioGroup में android:checkedButton क्षेत्र सेट अपने डिफ़ॉल्ट RadioButton की आईडी के साथ:

<RadioGroup 
    .... 
    android:checkedButton="@+id/button_1"> 

    <RadioButton 
     android:id="@+id/button_1" 
     ...../> 

    <RadioButton 
     android:id="@+id/button_2" 
     ...../> 

    <RadioButton 
     android:id="@+id/button_3" 
     ...../> 
</RadioGroup> 
0

यह viewId बिना RadioGroup

RadioButton radioBtn2 = new RadioButton(context); 

radioBtn2 की एक बग है और उत्पन्नViewId पर है CheildViewAdded()

public void onChildViewAdded(View parent, View child) { 
    if (parent == RadioGroup.this && child instanceof RadioButton) { 
     int id = child.getId(); 
     // generates an id if it's missing 
     if (id == View.NO_ID) { 
      id = View.generateViewId(); 
      child.setId(id); 
     } 
     ((RadioButton) child).setOnCheckedChangeWidgetListener(
       mChildOnCheckedChangeListener); 
    } 

    if (mOnHierarchyChangeListener != null) { 
     mOnHierarchyChangeListener.onChildViewAdded(parent, child); 
    } 
} 

तो, पहले रेडियो समूह .addView (रेडियोबीटीएन 2), फिर रेडियोबीटीएन 2.सेट चेक (सत्य);

इस तरह:

RadioGroup radioGroup = new RadioGroup(context); 
RadioButton radioBtn1 = new RadioButton(context); 
RadioButton radioBtn2 = new RadioButton(context); 
RadioButton radioBtn3 = new RadioButton(context); 

radioBtn1.setText("Less"); 
radioBtn2.setText("Normal"); 
radioBtn3.setText("More"); 

radioGroup.addView(radioBtn1); 
radioGroup.addView(radioBtn2); 
radioGroup.addView(radioBtn3); 

radioBtn2.setChecked(true); 
संबंधित मुद्दे