2024.04.18

2024. 4. 18. 22:45TIL(Today I Learned)

BMI 계산기 - ResultActivity.kt

 

package com.example.mybmi_calculator

import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import kotlin.math.pow
import kotlin.math.round

class ResultActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_result)

		// HEIGHT, WEIGHT 값을 정의함
        val height = intent.getIntExtra("height", 0)
        val weight = intent.getIntExtra("weight", 0)

        //BMI 계산
        var value = weight / (height/100.0).pow(2.0)
        value = round(value*10)/10

        var resultText = ""
        var resImage = 0
        var resColor = 0
		
        
        //if문을 활용해 BMI case별로 결과 텍스트의 문구와 이미지를 다르게 지정
        if(value < 18.5) {
            resultText = "저체중"
            resImage = R.drawable.lv1
            resColor = Color.YELLOW
        } else if(value >= 18.5 && value < 23.0) {
            resultText = "정상"
            resImage = R.drawable.lv2
            resColor = Color.GREEN
        } else if(value >= 23.0 && value < 25.0) {
            resultText = "과체중"
            resImage = R.drawable.lv3
            resColor = Color.BLACK
        } else if(value >= 25.0 && value < 30.0) {
            resultText = "경도 비만"
            resImage = R.drawable.lv4
            resColor = Color.CYAN
        } else if(value >= 30.0 && value < 35.0) {
            resultText = "중경도 비만"
            resImage = R.drawable.lv5
            resColor = Color.MAGENTA
        } else {
            resultText = "고도 비만"
            resImage = R.drawable.lv6
            resColor = Color.RED
        }

		/ findViewById 기능을 활용해 Id로 값들을 호출
        val tv_resValue = findViewById<TextView>(R.id.tv_resValue)
        val tv_resText = findViewById<TextView>(R.id.tv_resText)
        val iv_Image = findViewById<ImageView>(R.id.iv_image)
        val btn_back = findViewById<Button>(R.id.btn_back)

        tv_resValue.text = value.toString()
        tv_resText.text = resultText
        iv_Image.setImageResource(resImage)
        tv_resText.setTextColor(resColor)

		/뒤로가기 버튼 활성화
        btn_back.setOnClickListener{
            finish()
        }

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }
}