Android- BMI 수치 계산기 만들기(3)

Code_Alpacat·2021년 11월 3일

안드로이드 기초

목록 보기
11/18

이번엔 기능 및 동작에 실질적으로 관련된 MainActivity.kt을 다뤄보겠다.

버튼과 EditText같은 기능들을 동작하도록 하기위해서는 주소인 고유 Id값을 부여해주고 코틀린으로 동작시켜야한다.

형태는 다음과 같다.

android:id="@+id/heightEditText"

이 주소값은 mainactivity.kt 에서 주소값을 불러와 사용한다.

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 = findViewById<EditText>(R.id.weightEditText)

        val resultButton: Button = findViewById(R.id.resultButton)


    }
}
  • 위 코드에 보이듯 findViewById(R.id.값)를 이용해서 불러왔음을 알 수 있다. 이 형태와는 별개로 다음줄과 같이findViewById<>형태로 사용할 수도 있다.
  • R.id.heightEditText에서 heightEditText에 커서를 올리면, 주소값이 뜨는데, 주소값을 숫자로 부르려면 복잡하고, 모든 경로를 알기도 힘들기때문에, R.id라는 기능을 이용하는 것이다.
profile
In the future, I'm never gonna regret, cuz I've been trying my best for every single moment.

0개의 댓글