반응형
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
<?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=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edittext1"
android:hint="숫자1 입력"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edittext2"
android:hint="숫자2 입력"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num0"
android:text="0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num1"
android:text="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num2"
android:text="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num3"
android:text="3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num4"
android:text="4"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num5"
android:text="5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num6"
android:text="6"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num7"
android:text="7"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num8"
android:text="8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/num9"
android:text="9"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnsum"
android:text="더하기"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnsub"
android:text="빼기"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnmul"
android:text="곱하기"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btndiv"
android:text="나누기"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="30sp"
android:textColor="#ff0000"
android:id="@+id/value"
android:text="계산결과 : "/>
</LinearLayout>
|
cs |
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
|
package com.example.study002
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
lateinit internal var edittext1: EditText
lateinit internal var edittext2: EditText
lateinit internal var btnsum: Button
lateinit internal var btnsub: Button
lateinit internal var btnmul: Button
lateinit internal var btndiv: Button
lateinit internal var textview: TextView
lateinit internal var num1: String
lateinit internal var num2: String
internal var result: Int? = null
internal var numbuttons = ArrayList<Button>(10)
internal var numbtns = arrayOf(
R.id.num0, R.id.num1, R.id.num2, R.id.num3, R.id.num4, R.id.num5,
R.id.num6, R.id.num7, R.id.num8, R.id.num9
)
internal var I: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
edittext1 = findViewById(R.id.edittext1)
edittext2 = findViewById(R.id.edittext2)
btnsum = findViewById(R.id.btnsum)
btnsub = findViewById(R.id.btnsub)
btnmul = findViewById(R.id.btnmul)
btndiv = findViewById(R.id.btndiv)
textview = findViewById<TextView>(R.id.value)
btnsum.setOnTouchListener { view, motionEvent ->
num1 = edittext1.text.toString()
num2 = edittext2.text.toString()
result = Integer.parseInt(num1) + Integer.parseInt(num2)
textview.text = "계산결과 : " + result.toString()
false
}
btnsub.setOnTouchListener { view, motionEvent ->
num1 = edittext1.text.toString()
num2 = edittext2.text.toString()
result = Integer.parseInt(num1) - Integer.parseInt(num2)
textview.text = "계산결과 : " + result.toString()
false
}
btnmul.setOnTouchListener { view, motionEvent ->
num1 = edittext1.text.toString()
num2 = edittext2.text.toString()
result = Integer.parseInt(num1) * Integer.parseInt(num2)
textview.text = "계산결과 : " + result.toString()
false
}
btndiv.setOnTouchListener { view, motionEvent ->
num1 = edittext1.text.toString()
num2 = edittext2.text.toString()
result = Integer.parseInt(num1) / Integer.parseInt(num2)
textview.text = "계산결과 : " + result.toString()
false
}
for (i in 0..9) {
numbuttons.add(findViewById(numbtns[i]))
}
println(numbtns.size)
for (i in 0..numbtns.size - 1) {
numbuttons[i].setOnClickListener {
if (edittext1.isFocused == true) {
num1 = edittext1.text.toString() + numbuttons[i].getText().toString()
edittext1.setText(num1)
} else if (edittext2.isFocused == true) {
num2 = edittext2.text.toString() + numbuttons[i].getText().toString()
edittext2.setText(num2)
} else {
Toast.makeText(applicationContext, "먼저 텍스트를 선택", Toast.LENGTH_SHORT).show()
}
}
}
}
}
|
cs |
출처 : 코틀린을 활용한 안드로이드 프로그래밍
반응형
'코틀린study' 카테고리의 다른 글
[코틀린] 아날로그 디지털 시계 / time / data / xml (0) | 2021.08.09 |
---|---|
[코틀린] 버전 에러 문제 build.gradle (Module:app) (0) | 2021.08.09 |
[코틀린] 애완동물 보기 프로그램 / checkbox / Radio Button / kotlin (0) | 2021.08.09 |
[코틀린] 체크박스/스위치/이미지/버튼 (0) | 2021.08.07 |
[코틀린] 간단 계산기 (0) | 2021.08.07 |