কম্পিউটার টিউটোরিয়াল

Android-এ Radio Group কী এবং এর ব্যবহার

পরিচয়

উদাহরণের আগে জানা দরকার Android-এ Radio Group কী। Radio Group ラジオ বাটনের একটি গ্রুপ ধারণ করে। রেডিও বাটন ব্যবহার করে ব্যবহারকারীর প্রয়োজন অনুযায়ী সিলেক্ট এবং আনসিলেক্ট করা যায়।

এই উদাহরণটি দেখাবে কীভাবে Android-এ Radio Group ব্যবহার করা যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে যান: File → New Project এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লেয়াউট XML কনফিগার করুন

res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="আপনার প্রিয় বিষয়টি নির্বাচন করুন"
        android:textSize="20sp"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <RadioButton
            android:id="@+id/adroid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android" />

        <RadioButton
            android:id="@+id/java"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Java" />

        <RadioButton
            android:id="@+id/cpp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CPP" />

        <RadioButton
            android:id="@+id/clan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C Programming" />

    </RadioGroup>

    <Button
        android:id="@+id/buton"
        android:text="OK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

উপরের কোডে চারটি রেডিও বাটন রয়েছে। ব্যবহারকারী বাটনে ক্লিক করলে চেক করা রেডিও বাটনের টেক্সট নেওয়া হবে।

ধাপ ৩: MainActivity.java-তে লজিক লিখুন

src/MainActivity.java ফাইলে নিচের কোডটি যোগ করুন:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    RadioButton radioButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button = findViewById(R.id.buton);
        final RadioGroup radioGroup = findViewById(R.id.radioGroup);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int clickedRadioButton = radioGroup.getCheckedRadioButtonId();
                radioButton = findViewById(clickedRadioButton);

                if (clickedRadioButton == -1) {
                    button.setError("Error");
                } else {
                    button.setError(null);
                    Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

ধাপ ৪: Manifest.xml পরিবর্তন করার প্রয়োজন নেই

অ্যাপ্লিকেশন চালান

আপনার Android ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Android Studio-তে প্রজেক্টের একটি Activity ফাইল খুলুন এবং টুলবারের Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন। ডিফল্ট স্ক্রিন প্রদর্শিত হবে:

Android-এ Radio Group কী এবং এর ব্যবহার

যেকোনো বিষয় নির্বাচন করুন, উদাহরণস্বরূপ Java নির্বাচন করা হয়েছে:

Android-এ Radio Group কী এবং এর ব্যবহার

এখন OK বাটনে ক্লিক করলে Radio Group-এ থেকে নির্বাচিত রেডিও বাটনের টেক্সট পাওয়া যাবে:

Android-এ Radio Group কী এবং এর ব্যবহার

উপরের ফলাফলে OK বাটনে ক্লিক করলে নির্বাচিত রেডিও বাটনের টেক্সট Toast-এ দেখাবে।

  1. Android-এ ScrollView ব্যবহার করার পদ্ধতি

  2. অ্যান্ড্রয়েডে Flubot ম্যালওয়্যার কী?

  3. স্টক অ্যান্ড্রয়েড আসলে কী?

  4. অ্যান্ড্রয়েড সিস্টেম WebView ব্যাখ্যা: এর ভূমিকা, গুরুত্ব, এবং আপডেট রাখার উপায়