변명은 만개 결과는 한개

[Kotlin] findViewById 하는 세가지 방법 본문

공부/Kotlin

[Kotlin] findViewById 하는 세가지 방법

노마십가 2020. 11. 7. 00:48
728x90
반응형

Kotlin 에서 findViewById

// 1
val navView1 = findViewById(R.id.nav_view) as BottomNavigationView

// 2
val navView2 = findViewById<BottomNavigationView>(R.id.nav_view)

// 3
val navView3: BottomNavigationView = findViewById(R.id.nav_view)

위와 같이 세가지 모두 사용 가능함.

단, 첫번째 방법으로 findViewById 시도 시 두번째 방법을 추천함.

사실,

apply plugin: 'kotlin-android-extensions'

를 사용하면 findViewById 할 필요도 없음.

message.text = "set text"

처럼.

728x90
반응형

'공부 > Kotlin' 카테고리의 다른 글

[Kotlin] class  (0) 2020.11.10
[Kotlin] Types, Properties, Fields  (0) 2020.11.07
[Kotlin] 시작 전  (0) 2020.08.20
[Kotlin] 시작  (0) 2019.05.10
[펌] Kotlin 진짜 최고!  (0) 2019.05.05