一个EventHandler类
public class EventHandler {
public void methodReferences(View v) {
Toast.makeText(v.getContext(), "方法绑定:", Toast.LENGTH_LONG).show();
}
// 其他方法...
}
一个布局文件
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="net.dou7.learndatabinding.models.User" />
<variable
name="handler"
type="net.dou7.learndatabinding.EventHandler" />
</data>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{handler::methodReferences}"
android:text="方法绑定" />
</layout>
一个Activity类
public class EventHandingDemo extends AppCompatActivity {
private EventHandingDemoBinding mBinding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置布局
mBinding = DataBindingUtil.setContentView(this, R.layout.event_handing_demo);
// 设置User类
mBinding.setUser(new User("女神", 22, false));
// 设置EventHandler
mBinding.setHandler(new EventHandler());
}
}
完成简单的事件绑定
官方详细说明
网友评论