Bu derste Android Studio’ da radioButton kullanımı ve if – else if – else koşul ifadelerinin kullanımını gösteren bir örnek oluşturacağız. Mobil uygulamamızda radioButton denetimleri ile seçilen işleme ait sonucu ekranda gösteren örneği oluşturacağız.
Örneğimizde kullanıcıdan iki sayı alarak, radioButton ile Toplama, Çıkarma, Çarpma, Bölme işlemlerinden hangisi seçiliyse o işlemi yaparak sonucu TextView üzerinde görüntüleyeceğiz.
Sayfa tasarımımız aşağıdaki gibi olacaktır.

main_activity.xml Dosyamızın içeriği:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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=".MainActivity"> <RadioGroup android:id="@+id/radioGroup" android:layout_width="0dp" android:layout_height="195dp" android:layout_marginStart="24dp" android:layout_marginTop="30dp" android:layout_marginEnd="24dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editTextNumber2"> <RadioButton android:id="@+id/radioButtonToplama" android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="true" android:text="Toplama" /> <RadioButton android:id="@+id/radioButtonCikarma" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Çıkarma" /> <RadioButton android:id="@+id/radioButtonCarpma" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Çarpma" /> <RadioButton android:id="@+id/radioButtonBolme" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Bölme" /> </RadioGroup> <EditText android:id="@+id/editTextNumber1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="30dp" android:layout_marginEnd="24dp" android:ems="10" android:hint="1. Sayıyı Girin." android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editTextNumber2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="16dp" android:layout_marginEnd="24dp" android:ems="10" android:hint="2. Sayıyı Girin." android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editTextNumber1" /> <Button android:id="@+id/buttonHesapla" android:layout_width="220dp" android:layout_height="70dp" android:layout_marginStart="100dp" android:layout_marginTop="20dp" android:layout_marginEnd="100dp" android:text="Hesapla" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/radioGroup" /> <TextView android:id="@+id/textViewSonuc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="10dp" android:text="Sonuç: " android:textSize="34sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/buttonHesapla" /> </androidx.constraintlayout.widget.ConstraintLayout> |
MainActivity.java dosyamız:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package com.example.radiobutton_ornek; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText etSayi1=(EditText) findViewById(R.id.editTextNumber1); EditText etSayi2=(EditText) findViewById(R.id.editTextNumber2); RadioButton rbTopla=(RadioButton) findViewById(R.id.radioButtonToplama); RadioButton rbCikar=(RadioButton) findViewById(R.id.radioButtonCikarma); RadioButton rbCarp=(RadioButton) findViewById(R.id.radioButtonCarpma); RadioButton rbBol=(RadioButton) findViewById(R.id.radioButtonBolme); Button btnHesapla=(Button) findViewById(R.id.buttonHesapla); TextView txtSonuc=(TextView) findViewById(R.id.textViewSonuc); btnHesapla.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double sonuc=0; double sayi1=Integer.parseInt(etSayi1.getText().toString()); double sayi2=Integer.parseInt(etSayi2.getText().toString()); if(rbTopla.isChecked()){ sonuc=sayi1+sayi2; } else if(rbCikar.isChecked()){ sonuc=sayi1-sayi2; } else if(rbCarp.isChecked()){ sonuc=sayi1*sayi2; } else{ sonuc=sayi1/sayi2; } txtSonuc.setText("Sonuç: "+sonuc); } }); } } |
Ekran Çıktısı:

