//신장과 몸무게에 값을 넣었을 때, 확인버튼 누르고 넘겨준다.
package com.android.bmi_calculator
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)
1 val heightEditText = findViewById<EditText>(R.id.et_height)
//신장을 입력하는 text와 변수 연결.
//변수 선언: heightEditText_ findViewByld: 찾아서 가져오겠다
//<타입: EditText>_(android:id="@+id/et_height": R.id.et_height)
val weightEditText = findViewById<EditText>(R.id.et_weight)
val submitButton = findViewById<Button>(R.id.btn_submit)
2 submitButton.setOnClickListener{
//버튼이 눌렀을때 {} 가 실행.
5 if(heightEditText.text.isEmpty()){
//값을 넣지않았을 때 에러 발생_ 예외처리를 해야함.
Toast.makeText(this, "신장을 입력해주세요.", Toast.LENGTH_SHORT).show()
//아래의 코드를 실행하지 않을 것이라는 것을 알려주는 방법_Toast
//"띄울 msg","얼마나 오래 띄울 것인가"
return@setOnClickListener
//아래의 코드를 실행하지 않을 것_return
}
if(weightEditText.text.isEmpty()){
Toast.makeText(this, "체중을 입력해주세요.", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
3 val height : Int = heightEditText.text.toString().toInt()
//신장에 값을 입력했을때 그 값을 받아와야 함
//변수선언: height_ 타입:Int
//heightEditText.text.toString().toInt()_ heightEditText에서 text를 꺼내와 Int타입으로 바꿈
val weight : Int = weightEditText.text.toString().toInt()
4 val intent = Intent(this, ResultActivity::class.java)
//intent_넘겨주는 코드
intent.putExtra("height", height)
//intent로 사용자가 입력한 키와 몸무게 넘김_intent.putExtra()
intent.putExtra("weight", weight)
startActivity(intent)
}
}
}
//키 or 몸무게 입력하지 않은 후 실행_ 입력해 달라는 문구 생성 완.
//둘다 입력한 후 실행_ 샘플 결과 이미지로 이동.(돌아가기x)
'연습장 > 실습' 카테고리의 다른 글
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계산기_ResultActivity.kt (0) | 2024.01.18 |