반응형
Notice
Recent Posts
Recent Comments
Link
불로구
안드로이드 스튜디오 - BMI 계산기 만들기 본문
반응형
MainActivity
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: EditText = findViewById(R.id.weightEditText)
val resultButton: Button = findViewById(R.id.resultButton)
resultButton.setOnClickListener{
Log.d("MainActivity", "Result버튼의 클릭되었음")
if(heightEditText.text.isEmpty() || weightEditText.text.isEmpty()) {
Toast.makeText(this, "빈 값이 있습니다.", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
val height: Double = heightEditText.text.toString().toDouble()
val weight: Double = weightEditText.text.toString().toDouble()
val intent = Intent(this, ResultActivity::class.java)
intent.putExtra("height", height)
intent.putExtra("weight", weight)
startActivity(intent)
}
}
}
activity_main
<?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:layout_marginBottom="50dp"
android:gravity="center"
android:text="비만도 계산기"
android:textColor="@color/custom_black"
android:textSize="30sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="당신의 키(cm)"
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:ems="10"
android:inputType="numberDecimal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="당신의 몸무게(kg)"
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="numberDecimal" />
<Button
android:id="@+id/resultButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="계산하기" />
</LinearLayout>
ResultActivity
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.getDoubleExtra("height", 0.0)
val weight = intent.getDoubleExtra("weight", 0.0)
val bmi:Double = weight / (height / 100).pow(2.0)
val resultText:String = when{
bmi >= 35.0 -> "고도 비만"
bmi >= 30.0 -> "중도 비만"
bmi >= 25.0 -> "경도 비만"
bmi >= 23.0 -> "과체중"
bmi >= 18.5 -> "정상"
else -> "저체중"
}
val resultValueTextView: TextView = findViewById(R.id.bmiResultTextView)
val resultStringTextView:TextView = findViewById(R.id.resultTextView)
resultValueTextView.text = bmi.toString()
resultStringTextView.text = resultText
}
}
activity_result
<?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:paddingBottom="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI : "
android:textColor="@color/custom_black"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/bmiResultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp"
android:textStyle="bold"
tools:text="23.111111" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="결과는 : "
android:textColor="@color/custom_black"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp"
android:textStyle="bold"
tools:text="과체중입니다." />
</LinearLayout>
</LinearLayout>
반응형
'프로그래밍 > 코틀린 안드로이드' 카테고리의 다른 글
안드로이드 스튜디오 - 액션바(ActionBar) 삭제 (0) | 2021.03.23 |
---|---|
안드로이드 스튜디오 - 로또 추첨기 만들기 (1) | 2021.03.22 |
[코틀린 / 안드로이드] - 안드로이드 생명주기(1) (0) | 2021.02.14 |
[코틀린 / 안드로이드] - 안드로이드의 4대 구성요소 (0) | 2021.02.14 |
[코틀린 / 안드로이드] - 개발환경 준비 (0) | 2021.02.14 |
Comments