개발공부/andorid

android#코틀린(Kotlin) 계산기 만들기

ssung85 2020. 11. 14. 22:57
728x90

안드로이드 공부 하면서 계산기를 만들어 보았다.

 

Calculator.kt 작성

package com.ssung85.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_calculator.*
import kotlinx.android.synthetic.main.activity_calculator.clear
import kotlinx.android.synthetic.main.activity_calculator.plus
import kotlinx.android.synthetic.main.activity_calculator.result
import kotlinx.android.synthetic.main.activity_sum.*


class Calculator : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_calculator)

        var a: String = ""
        var list = mutableListOf<String>()
        var b: Long = 0L
        var c: Long = 1L
        var d: Double = 1.0

        num1.setOnClickListener {
            all_result.setText(all_result.getText().append("1"))
            a = all_result.getText().toString()
        }

        num2.setOnClickListener {
            all_result.setText(all_result.getText().append("2"))
            a = all_result.getText().toString()
        }

        num3.setOnClickListener {
            all_result.setText(all_result.getText().append("3"))
            a = all_result.getText().toString()
        }

        num4.setOnClickListener {
            all_result.setText(all_result.getText().append("4"))
            a = all_result.getText().toString()
        }

        num5.setOnClickListener {
            all_result.setText(all_result.getText().append("5"))
            a = all_result.getText().toString()
        }

        num6.setOnClickListener {
            all_result.setText(all_result.getText().append("6"))
            a = all_result.getText().toString()
        }

        num7.setOnClickListener {
            all_result.setText(all_result.getText().append("7"))
            a = all_result.getText().toString()
        }

        num8.setOnClickListener {
            all_result.setText(all_result.getText().append("8"))
            a = all_result.getText().toString()
        }

        num9.setOnClickListener {
            all_result.setText(all_result.getText().append("9"))
            a = all_result.getText().toString()
        }

        num0.setOnClickListener {
            all_result.setText(all_result.getText().append("0"))
            a = all_result.getText().toString()
        }

        plus.setOnClickListener {
            all_result.setText(all_result.getText().append("+"))
            a = all_result.getText().toString()
        }

        minus.setOnClickListener {
            all_result.setText(all_result.getText().append("-"))
            a = all_result.getText().toString()
        }

        multiple.setOnClickListener {
            all_result.setText(all_result.getText().append("X"))
            a = all_result.getText().toString()
        }

        slush.setOnClickListener {
            all_result.setText(all_result.getText().append("/"))
            a = all_result.getText().toString()
        }

        result.setOnClickListener {
            b = 0
            c = 1
            d = 1.0
            if (list.size != 0) {
                list = mutableListOf<String>()
            }

            if (all_result.getText().contains("+")) {
                val str = all_result.getText().split("+")
                val a = str[0].toDouble()
                val z = a.toInt()
                for (i in 0..str.size - 1) {
                    if(i == 0){
                        list.add(z.toString())
                        b += list.get(i).toInt()
                    }else{
                        list.add(str[i])
                        b += list.get(i).toInt()
                    }

                }

                all_result.setText(String.format("%d", b))
            }

            if (all_result.getText().contains("-")) {
                val str = all_result.getText().split("-")
                val a = str[0].toDouble()
                val z = a.toLong()
                b = z
                for (i in 0..str.size - 1) {
                    list.add(str[i])
                    if (i != 0) {
                        b -= list.get(i).toInt()

                    }
                }

                all_result.setText(String.format("%d", b))
            }

            if (all_result.getText().contains("X")) {
                val str = all_result.getText().split("X")
                val a = str[0].toDouble()
                val z = a.toInt()

                for (i in 0..str.size - 1) {
                    if(i == 0){
                        list.add(z.toString())
                        c *= list.get(i).toInt()
                    }else{
                        list.add(str[i])
                        c *= list.get(i).toInt()
                    }

                }

                all_result.setText(String.format("%d", c))
            }

            if (all_result.getText().contains("/")) {
                val str = all_result.getText().split("/")
                d = str[0].toDouble()
                for (i in 0..str.size - 1) {
                    list.add(str[i])
                    if (i != 0) {
                        d /= list.get(i).toDouble()

                    }
                }

                all_result.setText(String.format("%.2f", d))
            }

        }

        clear.setOnClickListener {
            all_result.setText("")
        }




    }
}

 

 

activity_calculator.xml 작성

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".Calculator">

   <EditText
        android:id="@+id/all_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0027FF"
        android:textSize="50dp"
        android:padding="30dp"
        android:textStyle="bold"
        android:text="0"
        android:textColor="#ffffff"
        android:gravity="right"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:orientation="vertical">

            <Button
                android:id="@+id/num1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30dp"
                android:text="1" />

            <Button
                android:id="@+id/num2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30dp"
                android:layout_marginTop="20dp"
                android:text="2" />

            <Button
                android:id="@+id/num3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textSize="30dp"
                android:text="3" />

            <Button
                android:id="@+id/plus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="+"
                android:textSize="30dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/num4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30dp"
                android:text="4" />

            <Button
                android:id="@+id/num5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textSize="30dp"
                android:text="5" />

            <Button
                android:id="@+id/num6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textSize="30dp"
                android:text="6" />

            <Button
                android:id="@+id/minus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textSize="30dp"
                android:text="-" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/num7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30dp"
                android:text="7" />

            <Button
                android:id="@+id/num8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textSize="30dp"
                android:text="8" />

            <Button
                android:id="@+id/num9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textSize="30dp"
                android:text="9" />

            <Button
                android:id="@+id/multiple"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textSize="30dp"
                android:text="*" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/num0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30dp"
                android:text="0" />

            <Button
                android:id="@+id/clear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="C"
                android:textSize="30dp" />

            <Button
                android:id="@+id/slush"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="/"
                android:textSize="30dp" />

            <Button
                android:id="@+id/result"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textSize="30dp"
                android:text="=" />

        </LinearLayout>


    </LinearLayout>



</LinearLayout>

실행 화면

구동 영상

 

728x90