Android | RadioButton, RadioGroup örneği

Written by Super User. Posted in Android Anlatımlar

Android widget örneklerine RadioGroup, RadioButton örneğiyle devam edelim. Ekran görüntümüz;

Layout dosyamız main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dünyanın en ünlü plak ve gramofon markalarından biri olan &apos;Sahibinin Sesi&apos;nin logosunda hangi hayvan yer alır?"
        android:textSize="20dp" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Bülbül" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Baykuş" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Köpek" />
        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="At" />
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Son Kararım" />
  
</LinearLayout>

Aktivite sınıfımız MainActivity.java

package com.gokselguren.radiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	private RadioGroup rgCevaplar;
	private RadioButton rbSecilen;
	private Button bSonKararim;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		rgCevaplar = (RadioGroup) findViewById(R.id.radioGroup1);
		bSonKararim = (Button) findViewById(R.id.button1);
		
		bSonKararim.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//Seçilen radiobutton id'sini al
				int selectedId = rgCevaplar.getCheckedRadioButtonId();
	 
				// Hangi radiobutton olduğunu bul ve değişkenine ata
			        rbSecilen = (RadioButton) findViewById(selectedId);
	 
				if(rbSecilen.getText().toString().equals("Köpek")){
					//Cevabımız Köpek'ti
					Toast.makeText(getApplicationContext(), "Kazandınız!!", 0).show();
					
				}
				else{
					Toast.makeText(getApplicationContext(), "Kaybettiniz!!", 0).show();
					
				}
			}
		});
	}
}