본문 바로가기
모바일/Kotlin

[Kotlin] 코틀린 안드로이드 계산기 만들기 실습

by drCode 2021. 4. 27.
728x90
반응형

코틀린 안드로이드 계산기 실습

 

Empty Activity 생성

Calculator.kt

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

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

        val one : TextView = findViewById(R.id.one)
        val two : TextView = findViewById(R.id.two)
        val three  : TextView = findViewById(R.id.three)
        val four  : TextView = findViewById(R.id.four)
        val five : TextView = findViewById(R.id.five)
        val six : TextView = findViewById(R.id.six)
        val seven : TextView = findViewById(R.id.seven)
        val eight : TextView = findViewById(R.id.eight)
        val nine : TextView = findViewById(R.id.nine)
        val zero : TextView = findViewById(R.id.zero)
        val plus : TextView = findViewById(R.id.plus)
        val ca : TextView = findViewById(R.id.cancel)

        val result : TextView = findViewById(R.id.result)

        // new -> old (new + old)
        var new = "0"
        var old = "0"

        one.setOnClickListener{
            new = new + "1"
            result.setText(new)
        }

        two.setOnClickListener{
            new = new + "2"
            result.setText(new)
        }

        three.setOnClickListener{
            new = new + "3"
            result.setText(new)
        }

        four.setOnClickListener{
            new = new + "4"
            result.setText(new)
        }

        five.setOnClickListener{
            new = new + "5"
            result.setText(new)
        }

        six.setOnClickListener{
            new = new + "6"
            result.setText(new)
        }

        seven.setOnClickListener{
            new = new + "7"
            result.setText(new)
        }

        eight.setOnClickListener{
            new = new + "8"
            result.setText(new)
        }

        nine.setOnClickListener{
            new = new + "9"
            result.setText(new)
        }

        zero.setOnClickListener{
            new = new + "0"
            result.setText(new)
        }


        ca.setOnClickListener {
            new = "0"
            old = "0"
            result.setText("0")
        }

        plus.setOnClickListener {
            old = (old.toInt() + new.toInt()).toString()
            new = "0"
            result.setText(old)
        }
    }
}

 

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">

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#2196F3"
        android:gravity="right|center_vertical"
        android:text="0"
        android:textSize="50dp"
        android:padding="14dp"/>

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

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/one"
                android:text="1"
                android:textSize="50dp"
                android:gravity="center"

                android:textColor="#ffffff" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/four"
                android:text="4"
                android:textSize="50dp"
                android:gravity="center"
                android:textColor="#ffffff" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/seven"
                android:text="7"
                android:textSize="50dp"
                android:gravity="center"
                android:textColor="#ffffff" />
        </LinearLayout>

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/two"
                android:text="2"
                android:textSize="50dp"
                android:gravity="center"

                android:textColor="#ffffff" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/five"
                android:text="5"
                android:textSize="50dp"
                android:gravity="center"
                android:textColor="#ffffff" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="8"
                android:id="@+id/eight"
                android:textSize="50dp"
                android:gravity="center"
                android:textColor="#ffffff" />
        </LinearLayout>

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/three"
                android:text="3"
                android:textSize="50dp"
                android:gravity="center"

                android:textColor="#ffffff" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="6"
                android:id="@+id/six"
                android:textSize="50dp"
                android:gravity="center"
                android:textColor="#ffffff" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="9"
                android:id="@+id/nine"
                android:textSize="50dp"
                android:gravity="center"
                android:textColor="#ffffff" />
        </LinearLayout>

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/cancel"
                android:background="#E91E63"
                android:text="CA"
                android:textSize="50dp"
                android:gravity="center"

                android:textColor="#ffffff" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#673AB7"
                android:id="@+id/plus"
                android:text="+"
                android:textSize="50dp"
                android:gravity="center"
                android:textColor="#ffffff" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="0"
                android:id="@+id/zero"
                android:textSize="50dp"
                android:gravity="center"
                android:textColor="#ffffff" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity android:name=".Calculator">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

728x90
반응형

댓글