반응형

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

 

<MainActivity>

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
package com.cookandroid.myapplication
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "초간단 계산기(수정)"
 
        btnAdd.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() + edit2.text.toString().toFloat()}"
            }
        }
        btnSub.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() - edit2.text.toString().toFloat()}"
            }
        }
        btnMul.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() * edit2.text.toString().toFloat()}"
            }
        }
        btnDiv.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else if(edit2.text.toString().toFloat() == 0.0f){
                Toast.makeText(applicationContext, "0으로 나눌 수 없습니다.", Toast.LENGTH_SHORT).show()
            } else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() / edit2.text.toString().toFloat()}"
            }
        }
 
        btnR.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else if(edit2.text.toString().toFloat() == 0.0f){
                Toast.makeText(applicationContext, "0으로 나눌 수 없습니다.", Toast.LENGTH_SHORT).show()
            } else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() % edit2.text.toString().toFloat()}"
            }
        }
    }
}
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
58
59
60
61
62
63
64
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <EditText
        android:id="@+id/edit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="숫자1"/>
 
    <EditText
        android:id="@+id/edit2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="숫자2"/>
 
    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="더하기"
        android:layout_margin="10dp"/>
 
    <Button
        android:id="@+id/btnSub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="빼기"
        android:layout_margin="10dp"/>
 
    <Button
        android:id="@+id/btnMul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="곱하기"
        android:layout_margin="10dp"/>
 
    <Button
        android:id="@+id/btnDiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="나누기"
        android:layout_margin="10dp"/>
 
    <Button
        android:id="@+id/btnR"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="나머지값"
        android:layout_margin="10dp"/>
 
    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="계산 결과: "
        android:textSize="30dp"
        android:textColor="#FF0000"/>
 
 
 
</LinearLayout>
cs
반응형

+ Recent posts