2012-06-25 15 views
26

मेरे पास एक रेडियो समूह rg1 है और मैं चयनित रेडियो बटन का मान प्राप्त करना चाहता हूं।एंड्रॉइड चयनित रेडियो बटन का मूल्य प्राप्त करें

मुझे पता है कि मैं का उपयोग करके चयनित रेडियो बटन का id प्राप्त कर सकते हैं:

if(rg1.getCheckedRadioButtonId()!=-1) 
int id= rg1.getCheckedRadioButtonId() 

कि मुझे आईडी देता है, लेकिन मुझे लगता है कि बटन का मूल्य चाहते हैं।

उत्तर

60

आपको उस इंडेक्स पर रेडियो बटन प्राप्त करने की आवश्यकता है, फिर उस बटन के टेक्स्ट का मूल्य प्राप्त करें। नीचे इस कोड का प्रयास करें।

if(rg1.getCheckedRadioButtonId()!=-1){ 
    int id= rg1.getCheckedRadioButtonId(); 
    View radioButton = rg1.findViewById(id); 
    int radioId = radioGroup.indexOfChild(radioButton); 
    RadioButton btn = (RadioButton) rg1.getChildAt(radioId); 
    String selection = (String) btn.getText(); 
} 
+0

आप यह काम करता है धन्यवाद, मैं अपने जवाब – Totti

+4

स्वीकार करेंगे चेक किया गया है प्राप्त करने के लिए आईडी लौटे है GetCheckedRadio बटन से केवल रेडियो समूह के भीतर आईडी? आप बटन की आईडी क्यों नहीं प्राप्त कर सकते थे और उसके बाद इंडेक्सऑफ चाइल्ड और getChildAt – Rarw

+1

के अतिरिक्त चरणों को जोड़ने के रेडियोबटन इंस्टैड को प्राप्त करने के लिए अधिक सामान्य खोज ViewById का उपयोग करें int रेडियोId = radioGroup.indexOfChild (रेडियोबटन) पर रेडियो समूह क्या है; –

48

इस प्रयास करें:

RadioGroup rg = (RadioGroup)findViewById(R.id.youradio); 
String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString(); 
+0

धन्यवाद, मुझे पहले से ही एक जवाब मिला है, उम्मीद है कि मैं स्वीकृत उत्तरों के रूप में एक से अधिक उत्तरों का चयन कर सकता हूं :(, वास्तव में आपको धन्यवाद , आप हमेशा मेरी मदद करते हैं – Totti

+1

धन्यवाद। यदि आपको शून्य बीकस ऐप को क्रैश कर सकता है तो आपको वैध होना चाहिए। – pess0a

3
RadioGroup rg = (RadioGroup)findViewById(R.id.youradio); 
String radiovalue = (RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString(); 
0

मुझे लगता है कि आप इस

RadioGroup rg=(RadioGroup)findViewById(R.id.youradio); 
String radiovalue=(RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString(); 
0
RadioGroup bhktype_RadioGr = (RadioGroup)findViewById(R.id.bhkypeRadioGroup); 
int flatTypeId = bhktype_RadioGroup.getCheckedRadioButtonId(); 
String flat_type = ((RadioButton) findViewById(flatTypeId)).getText().toString(); 
1

एक पंक्ति कोड

String buisnesstype = ((RadioButton) rdtranscompany.findViewById(rdtranscompany.getCheckedRadioButtonId())).getText().toString(); 
0 प्रयास करना चाहिए
0

सरल जवाब एक पंक्ति

View v = yourView; // as a button 

String radiovalue = (RadioButton)v).getText().toString(); 
1
rb1=(RadioButton)findViewById(rg1.getCheckedRadioButtonId()); 

अब आप rb1.getText() उपयोग कर सकते हैं radiobutton पर पाठ कि

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