RadioButton in Android in Hindi
RGPV University / DIPLOMA_CSE / MOBILE COMPUTING
RadioButton in Android
RadioButton in Android
RadioButton in Android
Android में RadioButton एक महत्वपूर्ण UI widget है, जिसे हम विभिन्न विकल्पों के बीच चयन करने के लिए उपयोग करते हैं। यह एक प्रकार का button होता है जो किसी group के अंदर एक समय में केवल एक विकल्प का चयन करने की अनुमति देता है। RadioButton आमतौर पर RadioGroup
के अंदर उपयोग किया जाता है, ताकि उसमें से एक ही विकल्प को चुना जा सके। यह user interface को स्पष्ट और उपयोगकर्ता के लिए सरल बनाता है।
Features of RadioButton in Android
- Single Selection: RadioButton के समूह में से केवल एक RadioButton का चयन किया जा सकता है, जो इसे अन्य विकल्पों से अलग करता है।
- Checked State: RadioButton का "checked" state होता है, जो यह दर्शाता है कि चयनित RadioButton को चुना गया है। इसे
isChecked()
method से प्राप्त किया जा सकता है। - Integration with RadioGroup: RadioButton को RadioGroup के साथ जोड़ा जाता है, ताकि सभी विकल्पों में से एक ही चयनित किया जा सके।
- Customizable: RadioButton की appearance को customize किया जा सकता है, जैसे कि रंग, आकार, और text style, ताकि यह आपके ऐप की design से मेल खाता हो।
- Focusable and Clickable: RadioButton focusable और clickable होते हैं, जिससे उपयोगकर्ता आसानी से चयन कर सकता है।
Use Cases of RadioButton in Android
RadioButton का उपयोग कई स्थानों पर किया जा सकता है, जहां एक ही समूह के अंदर केवल एक विकल्प को चयनित करने की आवश्यकता होती है। इसके कुछ प्रमुख उपयोग निम्नलिखित हैं:
- Form Selection: यदि आपको किसी form में से एक विकल्प चुनने के लिए users से input लेना हो, तो RadioButton बहुत उपयोगी होता है। उदाहरण के लिए, "Gender" के लिए Male/Female/Other का चयन।
- Survey or Polls: जब आप किसी सर्वेक्षण या पोल में से एक विकल्प चुनने के लिए चाहते हैं, तो RadioButton का उपयोग किया जाता है।
- Settings Selection: एप्लिकेशन में settings को configure करने के लिए RadioButton का उपयोग किया जाता है, जैसे कि Dark Mode या Light Mode का चयन।
- Quiz Applications: Quiz एप्लिकेशन में, सवालों के विकल्पों के रूप में RadioButton का उपयोग किया जाता है। एक समय में केवल एक उत्तर चुना जा सकता है।
Creating RadioButton in XML in Android
RadioButton को Android XML में create करना बेहद आसान है। इसे आप सीधे अपने layout XML में जोड़ सकते हैं। उदाहरण के लिए:
<RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content">
<RadioButton android:id="@+id/radio_button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1"/>
<RadioButton android:id="@+id/radio_button_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2"/>
</RadioGroup>
यहां, RadioGroup
सभी RadioButtons को समूहित करता है। जब उपयोगकर्ता एक RadioButton चुनता है, तो अन्य विकल्पों का चयन स्वतः हट जाता है।
RadioButton vs CheckBox in Android
RadioButton और CheckBox दोनों ही विकल्प चयनित करने के लिए उपयोग किए जाते हैं, लेकिन इनमें कुछ महत्वपूर्ण अंतर होते हैं:
Feature | RadioButton | CheckBox |
---|---|---|
Selection | Only one selection allowed at a time in a group. | Multiple selections allowed. |
Use case | Used when one option needs to be selected from a group. | Used when multiple options can be selected independently. |
Grouping | Must be used inside a RadioGroup . |
Can be used independently or with multiple CheckBoxes. |
Appearance | Circle-shaped button. | Square-shaped button. |
RadioButton का उपयोग तब किया जाता है जब हमें केवल एक विकल्प का चयन करना हो, जबकि CheckBox का उपयोग तब किया जाता है जब उपयोगकर्ता को कई विकल्पों का चयन करने की अनुमति हो।
FAQs
RadioButton Android में एक UI widget है जिसका उपयोग users से एक ही समूह में से एक विकल्प चयनित करने के लिए किया जाता है। इसे आमतौर पर RadioGroup
के अंदर उपयोग किया जाता है, ताकि एक समय में केवल एक विकल्प चुना जा सके।
RadioButton का उपयोग तब किया जाता है जब हमें किसी समूह से एक ही विकल्प का चयन करना हो। उदाहरण के लिए, user से gender का चयन करवाना (Male/Female/Other), या किसी setting को configure करना।
RadioButton को XML में बनाने के लिए इसे RadioGroup
के अंदर जोड़ा जाता है। उदाहरण के लिए:
<RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content">
<RadioButton android:id="@+id/radio_button_1" android:text="Option 1"/>
<RadioButton android:id="@+id/radio_button_2" android:text="Option 2"/>
</RadioGroup>
RadioButton का उपयोग तब किया जाता है जब हमें एक ही समूह से केवल एक विकल्प का चयन करना हो, जबकि CheckBox का उपयोग तब किया जाता है जब उपयोगकर्ता को कई विकल्पों का चयन करने की अनुमति हो। RadioButton केवल एक विकल्प को चुनने की अनुमति देता है, जबकि CheckBox से multiple selections हो सकते हैं।
Technically, RadioButton को बिना RadioGroup के भी उपयोग किया जा सकता है, लेकिन इसे स्वतंत्र रूप से उपयोग करने पर केवल एक RadioButton ही कार्य करेगा। RadioGroup का उपयोग करने से यह सुनिश्चित होता है कि एक समय में केवल एक RadioButton का चयन किया जाए।
आप RadioGroup
से चयनित RadioButton को getCheckedRadioButtonId()
method के माध्यम से प्राप्त कर सकते हैं। उदाहरण के लिए:
int selectedRadioButtonId = radioGroup.getCheckedRadioButtonId();
RadioButton selectedRadioButton = findViewById(selectedRadioButtonId);