목차
- 인트로 (완성앱 & 구현 기능 소개)
- Linearlayout 이용하여 화면 그리기
- 화면을 예쁘게 꾸며보기
- 키와 몸무게 입력 화면 기능 구현하기 (1)
- 키와 몸무게 입력 화면 기능 구현하기 (2)
- 결과화면 기능 구현하기
- 아웃트로 (정리)
이 챕터를 통해 배우는 것
Layout 을 그리는 법
- LinearLayout 사용하기
- TextView의 속성들과 사용하는 방법
- EditText의 속성들과 사용하는 방법
- Button 사용하는 방법
Layout과 Kotlin 코드를 함께 사용하기
버그 수정해보기
Activity에 대해 알아보기
Kotlin 문법
- when 분기문
- 람다함수
BMI 계산하기
- 공식 = 몸무게(kg) / (키(m) * 키(m))
- 18.5 이하면 저체중
- 18.5 ~ 23은 정상체중
- 23 ~ 25는 과체중
- 25 ~ 30은 경도비만
- 30 ~ 35는 중정도비만
- 35 이상은 고도비만
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fastcampus.aop.part2.chapter01">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Aoppart2chapter01">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ResultActivity" />
</application>
</manifest>
MainActivity.kt
package fastcampus.aop.part2.chapter01
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val heightEditText: EditText = findViewById(R.id.heightEditText)
val weightEditText = findViewById<EditText>(R.id.weightEditText)
val resultButton = findViewById<Button>(R.id.resultButton)
resultButton.setOnClickListener {
Log.d("MainActivity", "ResultButton 이 클릭되었습니다.")
if (heightEditText.text.isEmpty() || weightEditText.text.isEmpty()) {
Toast.makeText(this, "빈 값이 있습니다.", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
// 이 아래로는 절대 빈 값이 올 수 없음.
val height: Int = heightEditText.text.toString().toInt()
val weight: Int = weightEditText.text.toString().toInt()
val intent = Intent(this, ResultActivity::class.java)
intent.putExtra("height", height)
intent.putExtra("weight", weight)
startActivity(intent)
}
}
}
ResultActivity.kt
package fastcampus.aop.part2.chapter01
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.pow
class ResultActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_result)
val height = intent.getIntExtra("height", 0)
val weight = intent.getIntExtra("weight", 0)
Log.d("ResultActivity", "height : $height, weight : $weight")
val bmi = weight / (height / 100.0).pow(2.0)
val resultText = when {
bmi >= 35.0 -> "고도 비만"
bmi >= 30.0 -> "중정도 비만"
bmi >= 25.0 -> "경도 비만"
bmi >= 23.0 -> "과체중"
bmi >= 18.5 -> "정상체중"
else -> "저체중"
}
val resultValueTextView = findViewById<TextView>(R.id.bmiResultTextView)
val resultStringTextView = findViewById<TextView>(R.id.resultTextView)
resultValueTextView.text = bmi.toString()
resultStringTextView.text = resultText
}
}
activity_main.xml
<?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"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/height"
android:textColor="@color/custom_black"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/heightEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:inputType="number" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/weight"
android:textColor="@color/custom_black"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/weightEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/resultButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="확인하기" />
</LinearLayout>
activity_result.xml
<?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:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/bmiResultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI : " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
tools:text="23.11111" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="결과는 : " />
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="과체중입니다." />
</LinearLayout>
</LinearLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="custom_black">#222222</color>
<color name="white">#FFFFFFFF</color>
</resources>
strings.xml
<resources>
<string name="app_name">aop-part2-chapter01</string>
<string name="height">신장</string>
<string name="weight">체중</string>
</resources>