Bu örnekte kullanıcıdan iki sayı isteyerek Button tıklandığında bu sayıları toplayan ve sonucu gösteren bir mobil uygulama oluşturacağız.
Tasarım:
activity_main.xml 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 |
<?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"> <EditText android:id="@+id/editTextSayi1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:hint="1. Sayıyı girin" android:ems="10" android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editTextSayi2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="2. Sayıyı girin" android:ems="10" android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editTextSayi1" /> <Button android:id="@+id/buttonTopla" android:layout_width="209dp" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Topla" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editTextSayi2" /> <TextView android:id="@+id/textViewSonuc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Toplam:" android:textSize="34sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/buttonTopla" /> </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 |
package com.example.toplama; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnTopla=findViewById(R.id.buttonTopla); EditText editSayi1=findViewById(R.id.editTextSayi1); EditText editSayi2=findViewById(R.id.editTextSayi2); TextView txtSonuc=findViewById(R.id.textViewSonuc); btnTopla.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int sayi1,sayi2,toplam; sayi1=Integer.parseInt(editSayi1.getText().toString()); sayi2=Integer.parseInt(editSayi2.getText().toString()); toplam=sayi1+sayi2; txtSonuc.setText("Toplam: "+toplam); } }); } } |
İlginizi Çekebilir: Android Studio Hesap Makinesi Tasarımı (UI Tasarım)
Ekran Çıktısı: