根据官方文档,发现点击事件并没有触发

<data>
<variable
name="user"
type="com.m.databindingdemo.model.User"/>
<variable
name="handlers"
type="com.m.databindingdemo.model.MyHandlers"/>
</data>

。。。

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="@{user.firstname}"
android:onClick="@{handlers::onClickFriend}"
/>

 

Handler:

class MyHandlers {

fun onClickFriend(view: View) {
Toast.makeText(view.context,"test method reference",Toast.LENGTH_LONG).show()
}
}

原因是需要绑定:

val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
binding.handlers = MyHandlers() //关键语句