ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

์•ˆ๋“œ๋กœ์ด๋“œ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์—์„œ onclick event๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ค‘ 2๊ฐ€์ง€๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค.

How to make onClick event in Android databinding

 

  1. Call function
  2. Set declared onClickListener
Call function
android:onClick="@{()->viewModel.doSomething()}"

1
2
3
4
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="@{()->viewModel.showToast()}" />
 

 

Set OnclickListener
android:onClick="@{viewModel.onClickToastListener}"
1
val onClickToastListener = View.OnClickListener { showToast() }
 

 

1
2
3
4
<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="@{viewModel.onClickToastListener}"