티스토리 뷰

8. 모바일

코틀린/안드로이드/2020-05-03/ 계산기 예제

패스트코드블로그 2020. 5. 3. 10:40
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
package com.example.application
 
 
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
 
 
 
 
class MainActivity : AppCompatActivity() {
 
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        class Calc {
            var num = 0
        }
 
 
        val calc = Calc()
        plus.setOnClickListener{
            val number = etext.text.toString()
            calc.num = Integer.parseInt(number)
            etext.setText("")
 
 
            Toast.makeText(this,
                "입력된 숫자 " + calc.num+"",Toast.LENGTH_LONG).show()
 
 
        }
        equal.setOnClickListener{
            var num1 = calc.num
            var num2 = Integer.parseInt(etext.text.toString())
            etext.setText((num1 + num2).toString())
        }
    }
}
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
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/etext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
 
    <Button
        android:id="@+id/plus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:text="+"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etext" />
 
 
    <Button
        android:id="@+id/minus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="22dp"
        android:text="-"
        app:layout_constraintStart_toStartOf="@+id/plus"
        app:layout_constraintTop_toBottomOf="@+id/plus" />
 
 
    <Button
        android:id="@+id/multiply"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:text="*"
        app:layout_constraintStart_toStartOf="@+id/minus"
        app:layout_constraintTop_toBottomOf="@+id/minus" />
 
 
    <Button
        android:id="@+id/divid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:text="/"
        app:layout_constraintStart_toStartOf="@+id/multiply"
        app:layout_constraintTop_toBottomOf="@+id/multiply" />
 
 
    <Button
        android:id="@+id/equal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginTop="26dp"
        android:text="="
        app:layout_constraintStart_toStartOf="@+id/divid"
        app:layout_constraintTop_toBottomOf="@+id/divid" />
 
 
    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="55dp"
        android:layout_marginTop="34dp"
        android:layout_marginEnd="55dp"
        android:gravity="center"
        android:textSize="50dp"
        android:text="계 산 기"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
cs
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함