초급 1.bmi계산기

mangyun·2021년 11월 2일
0

초급 앱개발

목록 보기
1/8

정리

  • ctrl + Alt + L 은 전체정렬

null이 될수도 있다 -> ?
null이 절대 안됨 -> !
ex) val heightEditText: EditText?

 val heightEditText: EditText = findViewById(R.id.heightEditText)  - 명시적표현
 val weightEditText = findViewById<EditText>(R.id.weightEditText) - 추론적표현
  • 람다식은 중괄호 표현

기록확인 = 로그캣 = Log(앱이 죽는 이유등을 알수있음)

resultButton.setOnClickListener{
          Log.d("MainActivity", "ResultButton 이 클릭되었습니다.")
          val height:Int = heightEditText.text.toString().toInt() // 먼저 문장으로 그다음 int
      }
  • 예외일 경우를 토스트메시지로 따로 만들어야함
 if (heightEditText.text.isEmpty() || weightEditText.text.isEmpty()) {
                Toast.makeText(this, "빈 값이 있습니다", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            // 이 아래로는 절대 빈 값이 올수 없음

현재의 main액티비티에서에서 result 액티비티를 실행시킴

  val intent = Intent(this, ResultActivity::class.java)
            startActivity(intent)

하지만 이렇게만 한다면 앱이 죽게되고, maifests에 resutl 액티비티도 추가해야함.

<activity android:name =".ResultActivity"/>

인텐트 관련 정보 - https://developer.android.com/guide/components/intents-filters?hl=ko

resultAcitivity로 가져가기 위해 mainactivity에서 intent 정보를 putExtra로 넣음

result에서 getIntExtra로 받음

val bmi = weight / (height / 100.0).pow(2.0) - 코틀린스럽게 제곱근함수사용

when 표현 사용

기본기능

  • gravity - 정렬
  • padding - 위젯 테두리로부터 위젯 안에의 내용사이의 여백, 예를들어 textView안에서 글자까지
  • margin - 위젯이 부모 레이아웃의 테두리로부터의 여백, textview의 여백
  • string이나 color는 리소스로 따로 지정
    버튼 아이디 추가 - android:id="@+id/heightEditText"
profile
기억보다는 기록을 하자.

0개의 댓글