एंड्रॉइड में RadioButton और RadioGroup का उपयोग
यह ट्यूटोरियल सिंगल-चॉइस सेलेक्शन के लिए रेडियो बटन्स का उपयोग करने, मान प्राप्त करने, और इवेंट हैंडलिंग को कवर करता है।
लेआउट (activity_main.xml)
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="अपना लिंग चुनें"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/radioGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/radioMale">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="पुरुष"
android:textSize="18sp"
android:layout_marginBottom="12dp" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="महिला"
android:textSize="18sp"
android:layout_marginBottom="12dp" />
<RadioButton
android:id="@+id/radioOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="अन्य"
android:textSize="18sp" />
</RadioGroup>
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="चयन दिखाएं" />
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private Button btnDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radioGender);
btnDisplay = findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(v -> showSelection());
// वैकल्पिक: रियल-टाइम लिस्टनर
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
RadioButton selected = findViewById(checkedId);
if (selected != null) {
// तत्काल प्रतिक्रिया (वैकल्पिक)
// Toast.makeText(this, "चुना गया: " + selected.getText(), Toast.LENGTH_SHORT).show();
}
});
}
private void showSelection() {
int checkedId = radioGroup.getCheckedRadioButtonId();
if (checkedId == -1) {
Toast.makeText(this, "कृपया एक विकल्प चुनें", Toast.LENGTH_SHORT).show();
return;
}
RadioButton selectedRadioButton = findViewById(checkedId);
String selectedText = selectedRadioButton.getText().toString();
Toast.makeText(this, "चयनित: " + selectedText, Toast.LENGTH_LONG).show();
}
}
प्रोग्रामेटिकली रेडियो बटन बनाना
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
String[] options = {"विकल्प 1", "विकल्प 2", "विकल्प 3"};
for (int i = 0; i < options.length; i++) {
RadioButton rb = new RadioButton(this);
rb.setText(options[i]);
rb.setId(View.generateViewId()); // यूनिक ID
rb.setTextSize(16);
if (i == 0) rb.setChecked(true); // पहला डिफ़ॉल्ट
radioGroup.addView(rb);
}
layout.addView(radioGroup);
स्ट्रिंग रिसोर्सेज (res/values/strings.xml)
<resources>
<string name="app_name">RadioButton उदाहरण</string>
<string name="radio_male">पुरुष</string>
<string name="radio_female">महिला</string>
<string name="radio_other">अन्य</string>
<string name="btn_display">दिखाएं</string>
</resources>
एडवांस्ड: RadioButton कस्टमाइज़ेशन
कस्टम ड्रॉएबल (बटन स्टाइल)
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="कस्टम स्टाइल"
android:button="@null"
android:drawableStart="@drawable/custom_radio_selector"
android:drawablePadding="8dp"
android:padding="12dp" />
res/drawable/custom_radio_selector.xml
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radio_checked" android:state_checked="true" />
<item android:drawable="@drawable/radio_unchecked" />
</selector>
RadioGroup के महत्वपूर्ण मेथड्स
| मेथड | विवरण |
|---|---|
getCheckedRadioButtonId() |
चुने गए बटन की ID वापस करता है (-1 यदि कोई नहीं) |
check(id) |
प्रोग्रामेटिकली चयन सेट करता है |
clearCheck() |
सभी चयन हटाता है |
setOnCheckedChangeListener() |
चयन बदलाव लिस्टनर |
मटेरियल डिजाइन: MaterialRadioButton
<com.google.android.material.radiobutton.MaterialRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="मटेरियल रेडियो"
app:buttonTint="@color/colorPrimary" />
बेस्ट प्रैक्टिसेज
- हमेशा
RadioGroupमें लपेटें म्यूचुअल एक्सक्लूजन के लिए - लेबल के लिए स्ट्रिंग रिसोर्सेज का उपयोग करें
- डिफ़ॉल्ट चयन सेट करें बेहतर UX के लिए
- एक्सेसिबिलिटी:
contentDescriptionजोड़ें यदि आइकन-ओनली - लंबी लिस्ट के लिए
SpinnerयाChipGroupपर विचार करें