와챠의 우당탕탕 개발 기록장
[안드로이드] 직접 풀어보기 4-3 본문
반응형
직접 풀어보기 4-3
[실습 4-1]을 다음과 같이 수정하라.
- 터치가 아닌 클릭으로 변경한다.
- 나머지값을 구하는 버튼을 추가한다.
- 값을 입력하지 않고 버튼을 클릭할 때 오류 메시지를 토스트 메시지로 나타낸다.
- 실숫값을 계산한다.
- 0으로 나누면 토스트 메시지를 나타내고 계산하지 않는다.

activity_main.mxl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
tools:context=".MainActivity" | |
android:orientation="vertical"> | |
<EditText | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/editNum1" | |
android:layout_margin="10dp" | |
android:hint="숫자1"/> | |
<EditText | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/editNum2" | |
android:layout_margin="10dp" | |
android:hint="숫자2"/> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/btnAdd" | |
android:backgroundTint="#D5D5D5" | |
android:textColor="#000000" | |
android:layout_margin="10dp" | |
android:text="더하기"/> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/btnSub" | |
android:backgroundTint="#D5D5D5" | |
android:textColor="#000000" | |
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:backgroundTint="#D5D5D5" | |
android:textColor="#000000" | |
android:text="곱하기"/> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/btnDiv" | |
android:layout_margin="5dp" | |
android:backgroundTint="#D5D5D5" | |
android:textColor="#000000" | |
android:text="나누기"/> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/btnRest" | |
android:layout_margin="10dp" | |
android:backgroundTint="#D5D5D5" | |
android:textColor="#000000" | |
android:text="나머지값"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/tvResult" | |
android:textSize="30dp" | |
android:textColor="#FF0000" | |
android:layout_margin="10dp" | |
android:text="계산 결과 : " /> | |
</LinearLayout> |
MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mytest | |
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() { | |
// 1, 위젯 변수 선언 | |
lateinit var editNum1 : EditText; lateinit var editNum2 : EditText | |
lateinit var btnAdd : Button; lateinit var btnSub : Button | |
lateinit var btnMul : Button; lateinit var btnDiv : Button | |
lateinit var btnRest: Button; lateinit var tvResult : TextView | |
lateinit var num1 : String; lateinit var num2 : String | |
var result : Double? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// 2, 위젯 변수와 위젯 id를 연결 | |
editNum1 = findViewById<EditText>(R.id.editNum1) | |
editNum2 = findViewById<EditText>(R.id.editNum2) | |
btnAdd = findViewById<Button>(R.id.btnAdd) | |
btnSub = findViewById<Button>(R.id.btnSub) | |
btnMul = findViewById<Button>(R.id.btnMul) | |
btnDiv = findViewById<Button>(R.id.btnDiv) | |
btnRest = findViewById<Button>(R.id.btnRest) | |
tvResult = findViewById<TextView>(R.id.tvResult) | |
// 3, 동작을 정의 | |
// 더하기 | |
btnAdd.setOnClickListener { | |
// 3-1, EditText의 값을 읽어 온다 | |
num1 = editNum1.text.toString() | |
num2 = editNum2.text.toString() | |
if (num1.trim() == "" || num2.trim() == "") { | |
Toast.makeText(applicationContext, "입력 값이 비었습니다.", Toast.LENGTH_SHORT).show() | |
} else { | |
// 3-2, 계산 | |
result = num1.toDouble() + num2.toDouble() | |
// 3-3, 계산된 결과를 TextView에 보여준다 | |
tvResult.text = "계산 결과 : " + result.toString() | |
} | |
} | |
// 빼기 | |
btnSub.setOnClickListener { | |
// 3-1, EditText의 값을 읽어 온다 | |
num1 = editNum1.text.toString() | |
num2 = editNum2.text.toString() | |
if (num1.trim() == "" || num2.trim() == "") { | |
Toast.makeText(applicationContext, "입력 값이 비었습니다.", Toast.LENGTH_SHORT).show() | |
} else { | |
// 3-2, 계산 | |
result = num1.toDouble() - num2.toDouble() | |
// 3-3, 계산된 결과를 TextView에 보여준다 | |
tvResult.text = "계산 결과 : " + result.toString() | |
} | |
} | |
// 곱하기 | |
btnMul.setOnClickListener { | |
// 3-1, EditText의 값을 읽어 온다 | |
num1 = editNum1.text.toString() | |
num2 = editNum2.text.toString() | |
if (num1.trim() == "" || num2.trim() == "") { | |
Toast.makeText(applicationContext, "입력 값이 비었습니다.", Toast.LENGTH_SHORT).show() | |
} else { | |
// 3-2, 계산 | |
result = num1.toDouble() * num2.toDouble() | |
// 3-3, 계산된 결과를 TextView에 보여준다 | |
tvResult.text = "계산 결과 : " + result.toString() | |
} | |
} | |
// 나누기 | |
btnDiv.setOnClickListener { | |
// 3-1, EditText의 값을 읽어 온다 | |
num1 = editNum1.text.toString() | |
num2 = editNum2.text.toString() | |
if (num1.trim() == "" || num2.trim() == "") { | |
Toast.makeText(applicationContext, "입력 값이 비었습니다.", Toast.LENGTH_SHORT).show() | |
} else { | |
if (num2.trim() == "0") { | |
Toast.makeText(applicationContext, "0으로 나머지 계산 불가합니다.", Toast.LENGTH_SHORT).show() | |
} else { | |
// 3-2, 계산 | |
result = num1.toDouble() / num2.toDouble() | |
// 3-3, 계산된 결과를 TextView에 보여준다 | |
tvResult.text = "계산 결과 : " + result.toString() | |
} | |
} | |
} | |
// 나머지값 | |
btnRest.setOnClickListener { | |
// 3-1, EditText의 값을 읽어 온다 | |
num1 = editNum1.text.toString() | |
num2 = editNum2.text.toString() | |
if (num1.trim() == "" || num2.trim() == "") { | |
Toast.makeText(applicationContext, "입력 값이 비었습니다.", Toast.LENGTH_SHORT).show() | |
} else { | |
if (num2.trim() == "0") { | |
Toast.makeText(applicationContext, "0으로 나머지 계산 불가합니다.", Toast.LENGTH_SHORT).show() | |
} else { | |
// 3-2, 계산 | |
result = num1.toDouble() % num2.toDouble() | |
// 3-3, 계산된 결과를 TextView에 보여준다 | |
tvResult.text = "계산 결과 : " + result.toString() | |
} | |
} | |
} | |
} | |
} |
코틀린, 파이썬을 동시에 배우니까... 헷갈릴 거라고 생각했는데
헷갈리는 거 보단 형변환 같이 간단한 메서드 이름이 뭔지 모르니깤ㅋㅋㅋ 답답^^!
반응형
'코딩 일기장 > Android(Kotlin)' 카테고리의 다른 글
[안드로이드] 4장 연습문제 9번 (0) | 2021.03.20 |
---|---|
[안드로이드] 4장 연습문제 8번 (0) | 2021.03.20 |
[안드로이드] 4장 연습문제 7번 (0) | 2021.03.20 |
[안드로이드] 직접 풀어보기 4-4 (0) | 2021.03.20 |
[안드로이드] 직접 풀어보기 4-2 (0) | 2021.03.20 |