Android Studio Radio Button Kullanımı

Android Studio Radio Button Kullanımı

Android Studio’da gelelim Radio Button kullanımına. Radio button web sayfalarındaki gibi çokça kullandığımız checkbox gibi bir işaret kutucuğu diyebiliriz. CheckBox’tan farkı ise checkbox’ta birden fazla işaretçiyi seçebilirken, Radio Button’da tek bir işaretçiyi seçmemiz gerekmektedir.  Kodlamaya Başlarsak

Öncelikle activity_main :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <RadioGroup

        android:id="@+id/radioSex"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" >

 

        <RadioButton

            android:id="@+id/radioMale"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Erkek"

            android:checked="true" />

 

        <RadioButton

            android:id="@+id/radioFemale"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Bayan" />

 

    </RadioGroup>

 

    <Button

        android:id="@+id/btnDisplay"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Gonder" />

 

</LinearLayout&gt

 

MainActivity kodlarımız:

Bir buton’da işaretli olan radio button’u aldık. Button’a tıklandığın’da radio Group’taki seçili ıd’yi aldık ve button’da bir Toast Mesajı ile gösterdik.

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

package com.umiitkose.egitim2;

 

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 {

 

    private RadioGroup rG;

    private RadioButton rB;

    private Button gonder;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

gonder= (Button) findViewById(R.id.gonder);

gonder.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

int selectedId = rG.getCheckedRadioButtonId();

rB= (RadioButton) findViewById(selectedId);

Toast.makeText(MainActivity.this, rB.getText(), Toast.LENGTH_SHORT).show();

} });

}

}

Uygulamanın Çalışması :

 

 

Henüz Yorum Yapılmamış, İlk Yorumu Siz Yapın

Yorum Yollayın