package com.android.bmi_calculator
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import kotlin.math.pow
//제곱pow
import kotlin.math.round
//소수점round
class ResultActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_result)
1 val height = intent.getIntExtra("height", 0)
//앞의 키와 몸무게의 값을 받는 코드를 입력해야 결과값을 만들 수 있음.
//변수선언: val height_ 인텐스에서 받음: intent.getIntExtra()
val weight = intent.getIntExtra("weight", 0)
2 var value = weight / (height / 100.0).pow(2.0)
//BMI계산 value_ height: cm를 m로 변환 후 제곱:pow()
value = round(value*10) / 10
//value_ 소수점 첫번째 자리까지
3 var resultText = ""
var resImage = 0
var resColor = 0
//BMI value에 대한 분기처리
4 if (value < 18.5) {
resultText = "저체중"
resImage = R.drawable.ic_level1
resColor = Color.YELLOW
} else if (value >= 18.5 && value < 23.0) {
resultText = "정상제충"
resImage = R.drawable.ic_level2
resColor = Color.GREEN
} else if (value >= 23.0 && value < 25.0) {
resultText = "과체중"
resImage = R.drawable.ic_level3
resColor = Color.BLACK
} else if (value >= 25.0 && value < 30.0) {
resultText = "경도 비만"
resImage = R.drawable.ic_level4
resColor = Color.CYAN
} else if (value >= 30.0 && value < 35.0) {
resultText = "중정도 비만"
resImage = R.drawable.ic_level5
resColor = Color.MAGENTA
} else {
resultText = "고도비만"
resImage = R.drawable.ic_level6
resColor = Color.RED
}
5 val tv_resValue = findViewById<TextView>(R.id.tv_resValue)
//실제로 표현하기 위해 각각 가져와서 연결하기_ findWiewByld
val tv_resText = findViewById<TextView>(R.id.tv_resText)
val iv_resImage = findViewById<ImageView>(R.id.iv_resImage)
6 tv_resValue.text = value.toString()
//연결해주었으니 값을 넣어줌. text이므로 toString으로 함.
tv_resText.text = resultText
tv_resText.setTextColor(resColor)
tv_resImage.setImageResource(resImage)
7 val submitButton = findViewById<Button>(R.id.btn_close)
//버튼 연결_findWiewByld
submitButton.setOnClickListener{
finish()
//종료.
}
}
}
'연습장 > 실습' 카테고리의 다른 글
MBTI테스트_ activity_main.xml (0) | 2024.01.23 |
---|---|
로또번호 생성기_ MainActivity.kt (0) | 2024.01.19 |
로또번호 생성기_ circle_bg.xml (0) | 2024.01.19 |
로또번호 생성기_activity_Main (0) | 2024.01.19 |
BMI계산기_MainActivity.kt (0) | 2024.01.18 |