点击事件
方式一
<variable
name="testClick"
type="android.view.View.OnClickListener" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{testClick}"
android:text="点击测试1" />
bindingBinding.setTestClick {
Toast.makeText(this,"点击测试1",Toast.LENGTH_SHORT).show()
}
方式二
<variable
name="mHandler"
type="com.example.databinding.MyClickHandle" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{mHandler::clickTest}"
android:text="点击测试2" />
bindingBinding.mHandler= MyClickHandle()
class MyClickHandle{
fun clickTest(view:View){
Toast.makeText(view.context,"点击测试2",Toast.LENGTH_SHORT).show()
}
}
方式三
<variable
name="clickHandle"
type="com.example.databinding.ClickHandle" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{clickHandle::clickView}"
android:text="点击测试3" />
interface ClickHandle{
fun clickView(view: View)
}
bindingBinding.clickHandle=object :ClickHandle{
override fun clickView(view: View) {
Toast.makeText(view.context,"点击测试3",Toast.LENGTH_SHORT).show()
}
}
网友评论