2012-04-11 47 views
5

मेरे पास रेडियो समूह में दो रेडियो बटन हैं। मेरे पास 2 एंड्रोड भी है: बटन चेकबॉक्स- जब रेडियो बटन को अचयनित किया जाता है और चेकबॉक्स_वी के लिए जब वह चेकबॉक्स का चयन करता है। मैंने यह भी सुनिश्चित किया कि एक विधि onRadioButtonClick यह सुनिश्चित करने के लिए कि केवल एक रेडियो बटन खींचा जा सके: चेकबॉक्स और दूसरे में checkbox_v है। मैं radio पर कैसे लागू कर सकता हूं यह करने के लिए क्लिक करें? कोई उपाय?एक समय में चयनित एक रेडियो बटन

public void onRadioButtonClick(View v) 
{ 
    RadioButton button = (RadioButton) v; 
    boolean checkBox1Selected; 
    boolean checkBox2Selected = false; 

    Toast.makeText(MainActivity.this, 
     button.getId()+ " was chosen."+R.id.wificheckBox, 
     Toast.LENGTH_SHORT).show(); 

    if (button.getId() ==R.id.wificheckBox) { 
     // Toggle status of checkbox selection 
     checkBox1Selected = radiowifiButton.isChecked(); 

     // Ensure that other checkboxes are not selected 
     if (checkBox2Selected) { 
      radiomobileButton.setChecked(false); 
      checkBox2Selected = false; 
     } 
     else if (button.getId() ==R.id.wifimobilecheckBox) { 
      // Toggle status of checkbox selection 
      checkBox2Selected = radiomobileButton.isChecked(); 
      // Ensure that other checkboxes are not selected 
      if (checkBox1Selected) { 
       radiowifiButton.setChecked(false); 
       checkBox1Selected = false; 
      } 
     } 
    } 

मुख्य एक्सएमएल

<RadioGroup 
    android:id="@+id/radioGroup" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ll_1" 
    android:layout_marginLeft="20dp"> 

<LinearLayout 
    android:id="@+id/ll_2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ll_1" 
    android:layout_marginLeft="20dp" 
    android:orientation="horizontal" > 

    <RadioButton 
     android:id="@+id/wifimobilecheckBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/button_radio" 
     android:checked="true" 
     android:onClick="onRadioButtonClick" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/wificheckBox" 
     android:layout_toRightOf="@+id/wificheckBox" 
     android:paddingLeft="15dp" 
     android:text="WiFi or mobile network" 
     android:textColor="#333333" 
     android:textSize="20dp" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/ll_3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ll_2" 
    android:paddingTop="20dp" 
    android:layout_marginLeft="20dp" 
    android:orientation="horizontal" > 

    <RadioButton 
     android:id="@+id/wificheckBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/button_radio" 

     android:onClick="onRadioButtonClick" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/wificheckBox" 
     android:layout_toRightOf="@+id/wificheckBox" 
     android:paddingLeft="15dp" 
     android:text="WiFi " 
     android:textColor="#333333" 
     android:textSize="20dp" /> 
</LinearLayout> 

</RadioGroup> 

Drawabe- button_radio

<item android:state_checked="true"android:state_pressed="false" android:drawable="@drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="@drawable/checkbox"/> 
+0

आपने क्या प्रयास किया? अपना कोड यहां पेस्ट करें –

+0

किया गया। जोड़ा गया कोड – user182192

उत्तर

13

वे एक RadioGroup में हैं, तो केवल एक ही एक समय में चुना जा सकता है। यदि आप दोनों को चुनने में सक्षम होना चाहते हैं, तो उन्हें रेडियोग्रुप से हटा दें।

+10

गलत। मैं रेडियो बटन को अपने स्वयं के ड्रॉ करने योग्य ड्रॉबल/चेकबॉक्स और मानक के बजाय drawable/checkbox_v के साथ अनुकूलित कर रहा हूं। वर्तमान में मैं दोनों रेडियो बटन का चयन कर सकता हूं और दोनों छवि खींचने योग्य/checkbox_v सेट कर रहे हैं – user182192

0

आपको अपने ऑनडैडियो बटन पर क्लिक करना चाहिए और अपने रेडियोग्रुप पर ऑन चेक किए गए चेंजलिस्टर को जोड़ना चाहिए। http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

मुझे लगता है कि अपने स्वयं के क्लिक हैंडलर को लागू करके आप रेडियोग्रुप की कुछ कार्यक्षमता को ओवरराइट कर रहे हैं।

0

आपकी आवश्यकता स्पष्ट नहीं है। क्या आप इसे आसान बनाना चाहते हैं। मैंने आपकी आवश्यकताओं को समझने के लिए अपनी पूरी कोशिश की और ये नीचे दिए गए निष्कर्ष हैं :(

एक रेडियो बटन में टेक्स्ट हो सकता है। इसलिए आपको अपने लेआउट में TextView जोड़ने की ज़रूरत नहीं है। कृपया उन्हें हटाएं और एक सरल जोड़ें अपने radiobutton को android:text="blah.. blah.."

मेरी समझ के साथ

आप दो शक है।

कस्टम ड्रॉएबल ??

चेकआउट जवाब here साथ radiobuttons अनुकूलित करने के लिए कैसे।

पहले चयनित रेडियोबूटन को अचयनित करने के लिए कैसे ??

सच्चाई यह है कि आपको इसे मैन्युअल रूप से करने की ज़रूरत नहीं है क्योंकि आप उन रेडियोधन को एक ही रेडियोग्रुप में रखते हैं।

आशा है कि यह आपकी मदद करेगा।

0

जांच करने के लिए/अचिह्नित रेडियो बटन एक RadioGroup का हिस्सा नहीं, RadioButton गुण 'onClick' के तहत एंड्रॉयड जीयूआई डिजाइनर में एक विधि जोड़ (इस उदाहरण radio1_onClick में)।

RadioButton

RadioButton 
    [blah blah blah] 
    android:id="@+id/radio1" 
    android:onClick="radio1_onClick" 

अपने activity_main.xml आप निम्न विधि लिखना आरंभ करेगा के लिए

एक्सएमएल के तहत अपने activity.xml में।

private void radio1_onClick(View v) 
{ 
    CompoundButton b=(CompoundButton)findViewById(R.id.radio1); 
    b.setChecked(true); /* light the button */ 

    //b.setChecked(false); /* unlight the button */ 
} 
3

जैसा कि शीर्ष वोट दिया गया जवाब मेरे लिए कई अन्य लोगों के लिए काम नहीं करता है। मैं उस विधि को साझा कर रहा हूं जिसका मैंने उपयोग किया और यह पूरी तरह से काम कर रहा है।

<RadioGroup 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checkedButton="@+id/rb_female" 
      android:orientation="horizontal"> 

      <RadioButton 
       android:id="@+id/rb_female" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginEnd="10dp" 
       android:layout_marginRight="10dp" 
       android:button="@drawable/selector_radio_button" 
       android:text="Female" /> 

      <RadioButton 
       android:id="@+id/rb_male" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:button="@drawable/selector_radio_button" 
       android:text="Male" /> 
</RadioGroup> 

स्थापना android:checkedButton="@+id/rb_female"RadioGroup में मेरे लिए काम किया।

संबंधित मुद्दे