본문 바로가기
르탄즈 5기

[르탄즈 5기] 사전캠프 1주차 강의 - BMI계산기(마무리)

by 박매트 2024. 1. 18.

xml 파일을 다 만들고 나서 

이제 xml 파일에 있는 컴포넌트 들과 연결하고자 kotlin 코드를 작성하였다.

코드를 github에 업로드할까..? 싶었지만 아직 기초이기에 안올렸다.

제대로 작동되는 것을 알 수 있었다. 디자인 생김새가 왜 저렇게 되는 지는 모르겠다만... 값은 잘 나왔으니까 ㅎㅎㅎ

 

 

원래 xml 코드로만 디자인을 디자인했어서 직접 마우스로 끌어당겨서 만든 건 간만인 것 같은..ㅎ

MainActivity와 ResultActivity 간에 intent 로 값을 주고 받았음을 알 수 있다. 

 

 

 

 

1주차 완.강!

 

 

아래는 소스코드

 

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/et_height"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:ems="10"
        android:gravity="center"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toStartOf="@+id/textView4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/textView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="신장"
        android:textColor="#000000"
        android:textSize="20sp"
        app:layout_constraintEnd_toStartOf="@+id/et_height"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_main_title" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:text="체중"
        android:textColor="#000000"
        android:textSize="20sp"
        app:layout_constraintEnd_toStartOf="@+id/et_weight"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/et_weight"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:ems="10"
        android:gravity="center"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="@+id/textView2"
        app:layout_constraintEnd_toStartOf="@+id/textView5"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="@+id/textView2" />

    <TextView
        android:id="@+id/tv_main_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:layout_marginBottom="2dp"
        android:text="BMI 계산기"
        android:textColor="#C64B4B"
        android:textSize="48sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/textView4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="cm"
        android:textColor="@color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="@+id/et_height"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/et_height"
        app:layout_constraintTop_toTopOf="@+id/et_height" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="kg"
        android:textColor="@color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="@+id/et_weight"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/et_weight"
        app:layout_constraintTop_toTopOf="@+id/et_weight" />
    <Button
        android:id="@+id/btn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="108dp"
        android:text="확인하러가기"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="147dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_result.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".ResultActivity">
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:layout_marginBottom="2dp"
        android:text="당신의 BMI 계산 결과는?"
        android:textColor="#C64B4B"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_resValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:text="28.5"
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title" />

    <TextView
        android:id="@+id/tv_resText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="비만입니다."
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_resValue" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="돌아가기"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_resText" />

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/iv_image"
        android:src="@drawable/image_lv1"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

MainActivity.kt

package com.android.myfirstapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
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)

        // xml 에 있는 요소들과 연결
        val heightEditText = findViewById<EditText>(R.id.et_height)
        val weightEdittext = findViewById<EditText>(R.id.et_weight)
        val submitButton = findViewById<Button>(R.id.btn_check)

        submitButton.setOnClickListener{
            // 비어있을 경우
            if(heightEditText.text.isEmpty()){
                Toast.makeText(this, "신장을 입력해주세요", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }


            if(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)
            // 메인 액티비티에서 result액티비티를 부름
        }
    }
}

ResultActivity.kt

package com.android.myfirstapp

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 org.w3c.dom.Text
import kotlin.math.pow
import kotlin.math.round

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)

        // height(cm) =>  m 로 바꿔줘야함. 따라서 100으로 나눠준다.

        //BMI 계산
        var value = weight / (height/100.0).pow(2.0)
        value = round(value*10)/10
        
        var resultText = ""
        var resImage = 0
        var resColor = 0
        
        if(value<18.5){
            resultText = "저체중"
            resImage = R.drawable.image_lv1
            resColor = Color.YELLOW
        } else if (value >=18.5 && value< 23.0){
            resultText = "정상 체중"
            resImage = R.drawable.image_lv2
            resColor = Color.GREEN
        } else if (value >=23.0 && value< 25.0){
            resultText = "과체중"
            resImage = R.drawable.image_lv3
            resColor = Color.BLACK
        } else if (value >=25.5 && value< 30.0){
            resultText = "경도 비만"
            resImage = R.drawable.image_lv4
            resColor = Color.CYAN
        }else if (value >=30.0 && value< 35.0){
            resultText = "중정도 비만"
            resImage = R.drawable.image_lv5
            resColor = Color.MAGENTA
        } else {
            resultText = "고도 비만"
            resImage = R.drawable.image_lv5
            resColor = Color.RED
        }
        
        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.toString()
        iv_image.setImageResource(resImage)
        tv_resText.setTextColor(resColor)
        
        btn_back.setOnClickListener{
            finish()
        }


    }
}