Android RadioGroup ve RadioButton Kullanımı

activity_test.xml dosyası

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>

<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Nerelisiniz?"
android:layout_marginTop="20dp">
</TextView>

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

<RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ankara">
</RadioButton>

<RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/radioButton1" android:text="Kırşehir">
</RadioButton>

<RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/radioButton2" android:text="Nevşehir">
</RadioButton>

<RadioButton android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/radioButton3" android:text="Eskişehir">
</RadioButton>

</RadioGroup>

<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/radioGroup1" android:text="Gönder">
</Button>

</RelativeLayout>

Test sınıfı

package com.examaple.deneme;

import android.app.Activity;
import android.os.Bundle;
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 Test extends Activity
{
Button cevapla;
RadioGroup radiogrubu;
RadioButton radio01;
RadioButton radio02;
RadioButton radio03;
RadioButton radio04;
TextView txtSoru;

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

radiogrubu=(RadioGroup)findViewById(R.id.radioGroup1);

radio01=(RadioButton)findViewById(R.id.radioButton1);
radio02=(RadioButton)findViewById(R.id.radioButton2);
radio03=(RadioButton)findViewById(R.id.radioButton3);
radio04=(RadioButton)findViewById(R.id.radioButton4);

txtSoru = (TextView) findViewById(R.id.textView1);
cevapla=(Button)findViewById(R.id.button1);
cevapla.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{

int secilenRadio=radiogrubu.getCheckedRadioButtonId();
String secilenText="";

switch(secilenRadio)
{
case R.id.radioButton1:
{
secilenText = radio01.getText().toString();
break;
}
case R.id.radioButton2:
{
secilenText = radio02.getText().toString();
break;
}
case R.id.radioButton3:
{
secilenText = radio03.getText().toString();
break;
}
case R.id.radioButton4:
{
secilenText = radio04.getText().toString();
break;
}
}
Toast.makeText(Test.this, secilenText , Toast.LENGTH_SHORT).show();
}
});

}
}

Bir cevap yazın