Herkese merhaba,

Bu yazımda sizlere Android Studio üzerinden Kotlin kullanarak hesap makinesi yapımını göstereceğim. İlk olarak projemizi Kotlin Support seçeneği işaretli bir şekilde oluşturalım. Gelelim xml dosyamıza :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:onClick="btn_number_clicked"
        android:text="1"
        app:layout_constraintBaseline_toBaselineOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_number_clicked"
        android:text="2"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_number_clicked"
        android:text="3"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:onClick="btn_number_clicked"
        android:text="4"
        app:layout_constraintBaseline_toBaselineOf="@+id/button5"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_number_clicked"
        android:text="5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_number_clicked"
        android:text="6"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toBottomOf="@+id/button11" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:onClick="btn_number_clicked"
        android:text="7"
        app:layout_constraintBaseline_toBaselineOf="@+id/button8"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_number_clicked"
        android:text="8"
        app:layout_constraintStart_toEndOf="@+id/button4"
        app:layout_constraintTop_toBottomOf="@+id/button4" />

    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_number_clicked"
        android:text="9"
        app:layout_constraintStart_toEndOf="@+id/button5"
        app:layout_constraintTop_toBottomOf="@+id/button12" />

    <Button
        android:id="@+id/button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_number_clicked"
        android:text="0"
        app:layout_constraintStart_toEndOf="@+id/button7"
        app:layout_constraintTop_toBottomOf="@+id/button7" />

    <Button
        android:id="@+id/button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="btn_number_clicked"
        android:text="+"
        app:layout_constraintBaseline_toBaselineOf="@+id/button3"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/button12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="btn_number_clicked"
        android:text="-"
        app:layout_constraintBaseline_toBaselineOf="@+id/button6"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/button13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="btn_number_clicked"
        android:text="*"
        app:layout_constraintBaseline_toBaselineOf="@+id/button9"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/button14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="btn_number_clicked"
        android:text="/"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button13" />

    <Button
        android:id="@+id/button15"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:onClick="btn_number_clicked"
        android:text="Hesapla"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button14" />

    <Button
        android:id="@+id/button16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_number_clicked"
        android:text="AC"
        app:layout_constraintBaseline_toBaselineOf="@+id/button14"
        app:layout_constraintStart_toEndOf="@+id/button8" />
</android.support.constraint.ConstraintLayout>

Burada butonlara tek tek onClick metodu atamak yerine tek bir fonksiyon kullanarak işlemleri yapmayı da göstereceğim. Aslında işleri oldukça kolaylaştırmışlar. Yukarıda görüldüğü üzere onClick metodu içerisine btn_number_clicked fonksiyonunu kullanıyoruz. Peki bu fonksiyon ne yapıyor?

fun btn_number_clicked(view : View){
    val clicked_button = view as Button
    when(clicked_button.id){
        R.id.button -> {
            if (sayi1str.equals("0"))
            {
                sayi1str = "1"
            }else{
                sayi1str += "1"
            }
            console.setText(sayi1str)
        }
        R.id.button2 -> {
            if (sayi1str.equals("0"))
            {
                sayi1str = "2"
            }else{
                sayi1str += "2"
            }
            console.setText(sayi1str)
        }
        R.id.button3 -> {
            if (sayi1str.equals("0"))
            {
                sayi1str = "3"
            }else{
                sayi1str += "3"
            }
            console.setText(sayi1str)
        }
        R.id.button4 -> {
            if (sayi1str.equals("0"))
            {
                sayi1str = "4"
            }else{
                sayi1str += "4"
            }
            console.setText(sayi1str)
        }
        R.id.button5 -> {
            if (sayi1str.equals("0"))
            {
                sayi1str = "5"
            }else{
                sayi1str += "5"
            }
            console.setText(sayi1str)
        }
        R.id.button6 -> {
            if (sayi1str.equals("0"))
            {
                sayi1str = "6"
            }else{
                sayi1str += "6"
            }
            console.setText(sayi1str)
        }
        R.id.button7 -> {
            if (sayi1str.equals("0"))
            {
                sayi1str = "7"
            }else{
                sayi1str += "7"
            }
            console.setText(sayi1str)
        }
        R.id.button8 -> {
            if (sayi1str.equals("0"))
            {
                sayi1str = "8"
            }else{
                sayi1str += "8"
            }
            console.setText(sayi1str)
        }
        R.id.button9 -> {
            if (sayi1str.equals("0"))
            {
                sayi1str = "9"
            }else{
                sayi1str += "9"
            }
            console.setText(sayi1str)
        }
        R.id.button10 -> {
            if (!sayi1str.equals("0")) {
                sayi1str += "0"
                console.setText(sayi1str)
            }
        }
        R.id.button11 -> {
            sayi1int = sayi1str.toInt()
            islem = "+"
            sayi1str = "0"
            console.setText("0")
        }
        R.id.button12 -> {
            sayi1int = sayi1str.toInt()
            islem = "-"
            sayi1str = "0"
            console.setText("0")
        }
        R.id.button13 -> {
            sayi1int = sayi1str.toInt()
            islem = "*"
            sayi1str = "0"
            console.setText("0")
        }
        R.id.button14 -> {
            sayi1int = sayi1str.toInt()
            islem = "/"
            sayi1str = "0"
            console.setText("0")
        }
        R.id.button15 -> {
            sayi2str = console.text.toString()
            sayi2int = sayi2str.toInt()
            if (islem.equals("+")){
                var sonuc : Int = sayi1int+sayi2int
                console.setText(sonuc.toString())
            }else if (islem.equals("-")){
                var sonuc : Int = sayi1int-sayi2int
                console.setText(sonuc.toString())
            }else if (islem.equals("*")){
                var sonuc : Int = sayi1int*sayi2int
                console.setText(sonuc.toString())
            }else if (islem.equals("/")){
                var sonuc : Int = sayi1int/sayi2int
                console.setText(sonuc.toString())
            }
        }
        R.id.button16 -> {
            sayi1int = 0
            sayi2int = 0
            sayi1str = "0"
            sayi2str = "0"
            console.setText("0")
        }

    }
}

Yukarıda görüldüğü üzere fonksiyon içerisinde buton id’leri, if-else yapısı şeklinde sorgulama işlemini gerçekleştiriyoruz.  Örnek verecek olursak button11 id’li butonumuz topla butonudur. Tıklandığı zaman aşağıdaki işlemleri yapmaktadır.

sayi1int = sayi1str.toInt()
islem = "+"
sayi1str = "0"
console.setText("0")

Bu işlem Kotlin’in bize sağladığı kolaylıklardan sadece bir tanesidir. Gelelim kodun tamamına :

package com.can.ders

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    var sayi1str : String = "0"
    var sayi2str : String = "0"
    var sayi1int : Int = 0
    var sayi2int : Int = 0
    lateinit var console : EditText
    var islem : String = ""


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        console = findViewById(R.id.editText)

    }

    fun btn_number_clicked(view : View){
        val clicked_button = view as Button
        when(clicked_button.id){
            R.id.button -> {
                if (sayi1str.equals("0"))
                {
                    sayi1str = "1"
                }else{
                    sayi1str += "1"
                }
                console.setText(sayi1str)
            }
            R.id.button2 -> {
                if (sayi1str.equals("0"))
                {
                    sayi1str = "2"
                }else{
                    sayi1str += "2"
                }
                console.setText(sayi1str)
            }
            R.id.button3 -> {
                if (sayi1str.equals("0"))
                {
                    sayi1str = "3"
                }else{
                    sayi1str += "3"
                }
                console.setText(sayi1str)
            }
            R.id.button4 -> {
                if (sayi1str.equals("0"))
                {
                    sayi1str = "4"
                }else{
                    sayi1str += "4"
                }
                console.setText(sayi1str)
            }
            R.id.button5 -> {
                if (sayi1str.equals("0"))
                {
                    sayi1str = "5"
                }else{
                    sayi1str += "5"
                }
                console.setText(sayi1str)
            }
            R.id.button6 -> {
                if (sayi1str.equals("0"))
                {
                    sayi1str = "6"
                }else{
                    sayi1str += "6"
                }
                console.setText(sayi1str)
            }
            R.id.button7 -> {
                if (sayi1str.equals("0"))
                {
                    sayi1str = "7"
                }else{
                    sayi1str += "7"
                }
                console.setText(sayi1str)
            }
            R.id.button8 -> {
                if (sayi1str.equals("0"))
                {
                    sayi1str = "8"
                }else{
                    sayi1str += "8"
                }
                console.setText(sayi1str)
            }
            R.id.button9 -> {
                if (sayi1str.equals("0"))
                {
                    sayi1str = "9"
                }else{
                    sayi1str += "9"
                }
                console.setText(sayi1str)
            }
            R.id.button10 -> {
                if (!sayi1str.equals("0")) {
                    sayi1str += "0"
                    console.setText(sayi1str)
                }
            }
            R.id.button11 -> {
                sayi1int = sayi1str.toInt()
                islem = "+"
                sayi1str = "0"
                console.setText("0")
            }
            R.id.button12 -> {
                sayi1int = sayi1str.toInt()
                islem = "-"
                sayi1str = "0"
                console.setText("0")
            }
            R.id.button13 -> {
                sayi1int = sayi1str.toInt()
                islem = "*"
                sayi1str = "0"
                console.setText("0")
            }
            R.id.button14 -> {
                sayi1int = sayi1str.toInt()
                islem = "/"
                sayi1str = "0"
                console.setText("0")
            }
            R.id.button15 -> {
                sayi2str = console.text.toString()
                sayi2int = sayi2str.toInt()
                if (islem.equals("+")){
                    var sonuc : Int = sayi1int+sayi2int
                    console.setText(sonuc.toString())
                }else if (islem.equals("-")){
                    var sonuc : Int = sayi1int-sayi2int
                    console.setText(sonuc.toString())
                }else if (islem.equals("*")){
                    var sonuc : Int = sayi1int*sayi2int
                    console.setText(sonuc.toString())
                }else if (islem.equals("/")){
                    var sonuc : Int = sayi1int/sayi2int
                    console.setText(sonuc.toString())
                }
            }
            R.id.button16 -> {
                sayi1int = 0
                sayi2int = 0
                sayi1str = "0"
                sayi2str = "0"
                console.setText("0")
            }

        }
    }
}

Bir sonraki yazımda görüşmek üzere…