Bu örnek, Android Studio Java’da basit bir hesap makinesi oluşturmak için gerekli kodları göreceksiniz. Uygulamayı adım adım yapacağız.
Adım 1: Android Studio’da yeni bir proje oluşturun, Dosya ⇒ Yeni Proje’ye gidin ve yeni bir proje oluşturmak için gerekli tüm ayrıntıları doldurun.
Adım 2: res/layout/activity_main.xml dosyasına aşağıdaki kodları ekleyin.
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 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp"> <TextView android:id="@+id/textResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="70dp" android:background="#008080" android:padding="5dp" android:text="Code4Example" android:textColor="#fff" android:textSize="24sp" android:textStyle="bold" /> <EditText android:id="@+id/editSayi1" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <EditText android:id="@+id/editSayi2" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/editSayi1" android:layout_centerInParent="true" /> <GridLayout android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:layout_below="@+id/editSayi2" android:columnCount="2" android:rowCount="2" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnTopla" android:text="+" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnCikar" android:text="-" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnCarp" android:text="*" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnBol" android:text="/" /> </GridLayout> </RelativeLayout> |
Adım 3: src/MainActivity.java dosyasına aşağıdaki kodları ekleyin.
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 | import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { EditText editSayi1,editSayi2; TextView textSonuc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editSayi1 = findViewById(R.id.editSayi1); editSayi2= findViewById(R.id.editSayi2); textSonuc = findViewById(R.id.textResult); } public void btnTopla(View view){ double sayi1 = Double.parseDouble(editSayi1.getText().toString()); double sayi2 = Double.parseDouble(editSayi2.getText().toString()); double sonuc = sayi1 + sayi2; textSonuc.setText(Double.toString(sonuc)); } public void btnCikar(View view){ double sayi1 = Double.parseDouble(editSayi1.getText().toString()); double sayi2 = Double.parseDouble(editSayi2.getText().toString()); double sonuc = sayi1 - sayi2; textSonuc.setText(Double.toString(sonuc)); } public void btnCarp(View view){ double sayi1 = Double.parseDouble(editSayi1.getText().toString()); double sayi2 = Double.parseDouble(editSayi2.getText().toString()); double sonuc = sayi1 * sayi2; textSonuc.setText(Double.toString(sonuc)); } public void btnBol(View view){ double sayi1 = Double.parseDouble(editSayi1.getText().toString()); double sayi2 = Double.parseDouble(editSayi2.getText().toString()); double sonuc = sayi1 / sayi2; textSonuc.setText(Double.toString(sonuc)); } } |
Ekran Çıktısı:












Yorum Yap