반응형
MainActivity.kt
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
|
package com.example.helloandroid
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
lateinit var edit1 : EditText; lateinit var edit2 : EditText
lateinit var btnAdd : Button; lateinit var btnSub : Button
lateinit var btnMul : Button; lateinit var btnDiv : Button
lateinit var TextResult : TextView
lateinit var num1 : String; lateinit var num2 : String
var result : Int? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "초간단 계산기"
edit1 = findViewById<EditText>(R.id.Edit1)
edit2 = findViewById<EditText>(R.id.Edit2)
btnAdd = findViewById<Button>(R.id.BtnAdd)
btnSub = findViewById<Button>(R.id.BtnSub)
btnMul = findViewById<Button>(R.id.BtnMul)
btnDiv = findViewById<Button>(R.id.BtnDiv)
TextResult = findViewById<TextView>(R.id.TextResult)
btnAdd.setOnTouchListener { view, motionEvent ->
num1 = edit1.text.toString()
num2 = edit2.text.toString()
result = Integer.parseInt(num1) + Integer.parseInt(num2)
TextResult.text = "계산결과 : " + result.toString()
false
}
btnSub.setOnTouchListener { view, motionEvent ->
num1 = edit1.text.toString()
num2 = edit2.text.toString()
result = Integer.parseInt(num1) - Integer.parseInt(num2)
TextResult.text = "계산결과 : " + result.toString()
false
}
btnMul.setOnTouchListener { view, motionEvent ->
num1 = edit1.text.toString()
num2 = edit2.text.toString()
result = Integer.parseInt(num1) * Integer.parseInt(num2)
TextResult.text = "계산결과 : " + result.toString()
false
}
btnDiv.setOnTouchListener { view, motionEvent ->
num1 = edit1.text.toString()
num2 = edit2.text.toString()
result = Integer.parseInt(num1) / Integer.parseInt(num2)
TextResult.text = "계산결과 : " + result.toString()
false
}
}
}
|
cs |
activity_main.xml
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
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Edit1"
android:layout_margin="10dp"
android:hint="숫자1"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Edit2"
android:layout_margin="10dp"
android:hint="숫자2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnAdd"
android:layout_margin="10dp"
android:text="더하기"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnSub"
android:layout_margin="10dp"
android:text="빼기"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnMul"
android:layout_margin="10dp"
android:text="곱하기"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnDiv"
android:layout_margin="10dp"
android:text="나누기"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/TextResult"
android:textSize="30dp"
android:textColor="#ff0000"
android:layout_margin="10dp"
android:text="계산 결과 : "/>
</LinearLayout>
|
cs |
출처 : 코틀린을 활용한 안드로이드 프로그래밍
반응형
'코틀린study' 카테고리의 다른 글
[코틀린] 버전 에러 문제 build.gradle (Module:app) (0) | 2021.08.09 |
---|---|
[코틀린] 애완동물 보기 프로그램 / checkbox / Radio Button / kotlin (0) | 2021.08.09 |
[코틀린] 체크박스/스위치/이미지/버튼 (0) | 2021.08.07 |
[코틀린] TextView / EditText / Button form (0) | 2021.08.06 |
[코틀린] 클래스 : 필드+메소드 = 인스턴스 (0) | 2021.08.06 |