Android Studio - Basit hesap makinesi (Toplama,çıkarma,çarpma,bölme)

Merhaba arkadaşlar bugün android studio kullanarak basit bir hesap makinesi yapıp programı çalıştıracağız.

Öncelikle xml dosyasını görelim

Layout olarak relative kullandık. ve içerisine toplam 10 adet field var bu fieldlar sırayla şöyle
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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="17dp"
        android:layout_marginTop="18dp"
        android:text="İlk Sayıyı Giriniz :" />

    <EditText
        android:id="@+id/et_sayi1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/textView"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_marginTop="66dp"
        android:text="İkinci Sayı :" />

    <EditText
        android:id="@+id/et_sayi2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_sayi1"
        android:layout_alignEnd="@+id/et_sayi1"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/btn_toplama"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_marginTop="160dp"
        android:text="Toplama" />

    <Button
        android:id="@+id/btn_cikarma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/btn_toplama"
        android:layout_toEndOf="@+id/btn_toplama"
        android:text="Çıkarma" />

    <Button
        android:id="@+id/btn_carpma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/btn_toplama"
        android:layout_toEndOf="@+id/btn_cikarma"
        android:text="Çarpma" />

    <Button
        android:id="@+id/btn_bolme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btn_toplama"
        android:layout_toEndOf="@+id/btn_carpma"
        android:text="Bölme" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_marginTop="108dp"
        android:text="Sonuç :" />

    <TextView
        android:id="@+id/tv_sonuc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_marginStart="0dp"
        android:layout_marginTop="-6dp"
        android:layout_toEndOf="@+id/textView"
        android:text="0"
        android:textSize="24sp" />
</RelativeLayout>



TextField : sadece bilgi amaçlı kullandıklarımız kullanıcının ne yapmasını veya o kısmın ne işe yaradığını belirtmek için yazı yazdığımız kısım.

Sayı 1 :

Sayı 2 :

Sonuç : yazan kısımlar bunlar

EditText: bu kısımlar ise kullanıcıdan sayı almak için işimize yarayacak olan alanlar.

Button: sayılar ile yapılacak işlemi seçmemize yarayacak kısım.




Şimdi de Java dosyamıza göz atalım.
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
package com.example.erdinyaan.basithesapmakinesi;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button toplama,cikarma,carpma,bolme;
    private TextView sonuc;
    private EditText sayi1,sayi2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toplama=(Button)findViewById(R.id.btn_toplama);
        cikarma=(Button)findViewById(R.id.btn_cikarma);
        carpma=(Button)findViewById(R.id.btn_carpma);
        bolme=(Button)findViewById(R.id.btn_bolme);

        sonuc=(TextView)findViewById(R.id.tv_sonuc);
        sayi1=(EditText)findViewById(R.id.et_sayi1);
        sayi2=(EditText)findViewById(R.id.et_sayi2);

        toplama.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sayi1.getText().toString().equals("") || sayi2.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this,"Lütfen sayı giriniz!",Toast.LENGTH_SHORT).show();
                }else{
                    double s1=Double.valueOf(sayi1.getText().toString());
                    double s2=Double.valueOf(sayi2.getText().toString());
                    double s3=s1+s2;

                    sonuc.setText(String.valueOf(s3));
                }
            }
        });
        
        cikarma.setOnClickListener(new View.OnClickListener(){
            public  void onClick(View v){
                if(sayi1.getText().toString().equals("")||sayi2.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this, "Lütfen Sayı giriniz!", Toast.LENGTH_SHORT).show();
                }else{
                    double s1=Double.valueOf(sayi1.getText().toString());
                    double s2=Double.valueOf(sayi2.getText().toString());
                    double s3=s1-s2;

                    sonuc.setText(String.valueOf(s3));

                }
            }
        });

        carpma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sayi1.getText().toString().equals("")||sayi2.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this, "Lütfen Sayı giriniz!", Toast.LENGTH_SHORT).show();
                }else{
                    double s1=Double.valueOf(sayi1.getText().toString());
                    double s2=Double.valueOf(sayi2.getText().toString());
                    double s3=s1*s2;

                    sonuc.setText(String.valueOf(s3));
                }
            }
        });

        bolme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sayi1.getText().toString().equals("")||sayi2.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this, "Lütfen Sayı giriniz!", Toast.LENGTH_SHORT).show();
                }else{
                    double s1=Double.valueOf(sayi1.getText().toString());
                    double s2=Double.valueOf(sayi2.getText().toString());
                    double s3=s1/s2;

                    sonuc.setText(String.valueOf(s3));
                }
            }
        });
    }
}



Kullanacak olduğumuz kütüphaneleri import ettikten sonra  değişkenlerimizi oluşturuyoruz bunlar

Button olarak toplama,cikarma,carpma ve bolme dir.

Sonucu göstermek için bir adete textView oluşturuyoruz bu da sonuc dur.

Sayıları alabilmek için de EditText kısımlarını oluşturuyoruz bunlar da sayi1 ve sayi2 dir.




Değişkenleri oluşturduktan sonra program oncreate(yani çalıştırılırken ) bu değişkenlerin içine bizim programımız üzerinde oluşturduğumuz değişkenlere eşitliyoruz ki işimiz kolay olsun.




Bu işlemi de yaptıktan sonra tek kalan butonlara OnClickListener eklemek.

OnclickListener butonların tıklanınca yapılacak olan işlemleri yazmamızı sağlayan butonumuz.

Kodlarda gördüğünüz gibi OnClickListener oluşturduktan sonra tek yapmamız gereken sayıları alıp işlemi yapmak ve çıkan sonucu da sonuc adlı TextView in içine kaydetmek.




Video ile görmek isterseniz : [wonderplugin_video videotype="mp4" mp4="https://drive.google.com/uc?export=download&id=1zs9evJvSZaQZnhXCNyen3_5u5jFDTtml" videowidth=600 videoheight=400 keepaspectratio=1 videocss="position:relative;display:block;background-color:#000;overflow:hidden;max-width:100%;margin:0 auto;" playbutton="http://www.erdincyasan.org/wp-content/plugins/wonderplugin-video-embed/engine/playvideo-64-64-0.png"]

Yorumlar

Bu blogdaki popüler yayınlar

.Net 6.0 ile ModelState kullanımı (RedirectToAction)

Delegate Prediction Func C#

Açıklama