Sponsored

অ্যান্ড্রয়েড রেডিও বাটন

0
41

মাঝে মাঝে আমাদের অ্যাপে ইউজার থেকে কোন ইনপুট নিতে হয়। যেমন ধরা যাকা কোন ইভেন্টে ইউজার কি যাবে কি যাবে না। এই কাজটা আমরা করতে পারি রেডিও বাটন দিয়ে। রেডিওবাটনের অপশন গুলো সব গুলো রাখতে হয় বাটন গ্রুপে। যেমন আমরা নিচের মত করে একটা লেয়াউট তৈরি করতে পারি নিচের মত করেঃ

এখানে আমরা একোটা টেক্সট ভিউ রেখেছি, রেডিওবাটন গ্রুপ রেখেছি আর একটা বাটন রেখেছি। কোড গুলো নিচের মত করে লিখতে পারিঃ

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="me.jakir.androidradiobutton.MainActivity">

    <TextView android:id="@+id/textView2" android:layout_width="368dp" android:layout_height="wrap_content" android:layout_marginLeft="32dp" android:layout_marginRight="32dp" android:layout_marginTop="32dp" android:text="Are you going?" android:textAlignment="center" android:textStyle="bold" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:orientation="vertical" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" android:id="@+id/radioGroup">

        <RadioButton android:id="@+id/yes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Yes" />

        <RadioButton android:id="@+id/no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="No" />
        <RadioButton android:id="@+id/maybe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Maybe" />
    </RadioGroup>

    <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:text="Check Result" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

</android.support.constraint.ConstraintLayout>

কনস্ট্রেন্ট লেয়াউট ব্যবহার করার কারণে অনেক গুলো বাড়তি কোড যুক্ত হয়েছে। মূল কোড হচ্ছে এরকমঃ

<RadioGroup android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
    <RadioButton android:id="@+id/yes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Yes" />
    <RadioButton android:id="@+id/no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="No" />
</RadioGroup>

এরপর যখন সাবমিট বাটনে ক্লিক করবে, আমরা চেক করব কোন রেডিও বাটনটি সিলেক্ট করা হয়েছে। getCheckedRadioButtonId() দিয়ে কোন বাটনটি চেক করা হয়েছে, তা পাওয়া যাবে। এরপর আমরা যে কোন কাজ করতে পারি। যেমন উদাহরণে ঐ বাটনের টেক্সট আমরা পড়ে টোস্টে দেখিয়েছি। সম্পূর্ণ কোডঃ

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

public class MainActivity extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton radioButton;
    Button result;


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

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        result = (Button) findViewById(R.id.button);

        result.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedButton = radioGroup.getCheckedRadioButtonId();
                radioButton = (RadioButton) findViewById(selectedButton);
                Toast.makeText(MainActivity.this, "You have selected: " +
                        radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }


}

সোর্স  কোড গুলো পাওয়া যাবে গিটহাবে।

Sponsored
Sponsored
Search
Categories
Read More
Tech
অনলাইন ই-লার্নিং প্ল্যাটফর্ম ওমক ডট কম- শিখুন/শেখান যে কেউ যেকোন জায়গায় যেকোন সময় বিনামূল্যে বা পয়সা দিয়ে | Techtunes | টেকটিউনস
মোবাইলের মাধ্যমে চ্যাট করার কিছু দারুণ সফটওয়্যার মো. আমিনুল ইসলাম সজীব
By Nettumi 2026-09-26 11:50:29 0 67
Tech
iOS বা আইফোনের জন্য অ্যাপ তৈরি করার গাইড লাইন
iOS বা আইফোনের জন্য অ্যাপ তৈরি করার জন্য সবার আগে যেটা লাগবে, তা হচ্ছে Mac OS X. আর তার জন্য...
By Nettumi 2026-09-30 18:42:02 0 55
Tech
আপনার কম্পিউটারে কি উইন্ডোজ ১০ আটোমেটিক শাটডাউন হয়ে যায়? প্রথম বার চালু করলে লগো বেসে আবার অফ হয়ে যায়। নিয়ে নিন সমাধান। | Techtunes | টেকটিউনস
মজিলার নির্বাচিত সাতটি বেস্ট ফায়ারফক্স 3 অ্যাডঅন দুঃসাহসী টিনটিন
By Nettumi 2026-09-25 03:41:10 0 58
Tech
Admobs থেকে প্রতিদিন click করে ১০ ডলার এর উপর আয় করবেন যেভাবে প্রমাণসহ | Techtunes | টেকটিউনস
‘নারী’ মানে প্রথম যে কথাটি আসে, তা হলো ‘মা’। Rubel Oron
By Nettumi 2026-09-25 04:00:35 0 48
Nulled
WP Project Manager Pro v3.0.2 - WordPress Plugin - Nulled PHP Scripts
WP Project Manager Pro v3.0.2 – WordPress Plugin Free Download. A powerful tool to Manage...
By Nettumi 2026-09-17 18:07:45 0 81
‎ ‎ ‎
Nettumi https://nettumi.com