Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में रेडियो बटन का उपयोग कैसे करें?

एंड्रॉइड में 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 पर विचार करें
  1. एंड्रॉइड में SharedPreferences का उपयोग कैसे करें? एंड्रॉइड में SharedPreferences का उपयोग कैसे करें?

    एंड्रॉइड में SharedPreferences — की-वैल्यू डेटा स्टोर करने का आसान तरीका SharedPreferences एंड्रॉइड में छोटे प्राइमिटिव डेटा (बूलियन, इंट, फ्लोट, लॉन्ग, स्ट्रिंग, स्ट्रिंग सेट) को XML फाइल में प्राइवेटली स्टोर करने का मानक API है। यह रिमेंबर मी लॉगिन, यूज़र सेटिंग्स, ऑनबोर्डिंग फ्लैग आदि के लिए आदर्

  1. Android में रेडियो बटन पर OnClickListener कैसे सेट करें? Android में रेडियो बटन पर OnClickListener कैसे सेट करें?

    Android में रेडियो बटन पर क्लिक लिस्टनर सेट करने का गाइड यह ट्यूटोरियल दिखाता है कि कैसे XML में android:onClick एट्रिब्यूट का उपयोग करके या Java कोड में रेडियो बटन पर क्लिक इवेंट हैंडल किया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं। चरण 2: लेआ

  1. Android में ScrollView का उपयोग कैसे करें? Android में ScrollView का उपयोग कैसे करें?

    Android में ScrollView का उपयोग यह उदाहरण दर्शाता है कि लंबी सामग्री को स्क्रॉल करने योग्य बनाने के लिए ScrollView का उपयोग कैसे करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project से प्रोजेक्ट बनाएं। चरण 2: ScrollView में कई बटन जोड़ें (activity_main.xml) <?xml version=1.0 e