android开发利器Data Binding简介和简单使用(已支持双向绑定)

来源:互联网 发布:剑网3七秀捏脸数据 编辑:程序博客网 时间:2024/05/16 02:03

不久前DataBinding已经支持双向绑定,所以我也没有理由再拒绝使用它。因为它实在是太方便了。
有了它我们不再需要findViewById,也不需要在定义一个个控件。

构建环境

如果想使用支持双向绑定的dataBinding,必须使用Android Studio 2.1 Preview 3版本及以上。必须要使用2.1-alpha3以上 android gradle plugin。在需要使用的module的build.gradle文件中添加:
android {    ...    dataBinding.enabled = true}``这样就配置完成了,很简单。## 示例 ##先来看看一个简单的示例:比如我们有一个登录界面需要填写用户名密码点击登录按钮进行登录,先来看看布局:
<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:sdf="http://schemas.android.com/apk/res-auto">    <data>        <variable            name="user"            type="com.aa.www.bean.User"></variable>    </data>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <LinearLayout            android:id="@+id/linearLayout"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_alignParentLeft="true"            android:layout_alignParentStart="true"            android:layout_alignParentTop="true"            android:orientation="vertical">            <LinearLayout                android:layout_width="match_parent"                android:layout_height="300dp"                android:layout_marginTop="20dp"                android:gravity="center"                android:orientation="vertical">                                            <EditText                            android:id="@+id/et_name"                            android:layout_width="match_parent"                            android:layout_height="match_parent"                            android:hint="用户名"                            android:inputType="number"                            android:maxLength="13"                            android:text="@{user.name}"                            android:textSize="@dimen/ts_normal" />                                                 <EditText                            android:id="@+id/et_pass"                            android:layout_width="match_parent"                            android:layout_height="match_parent"                            android:hint="密码"                            android:inputType="textPassword"                            android:maxLength="13"                            android:text="@{user.pass}"                            android:textSize="@dimen/ts_normal" />                              <Button                    android:id="@+id/btn_login"                    android:layout_width="match_parent"                    android:layout_height="40dp"                    android:onClick="login"                    android:text="登录"                />                <Button                    android:layout_width="match_parent"                    android:layout_height="40dp"                    android:onClick="register"                    android:text="注册"                    android:id="@+id/button2" />            </LinearLayout>        </LinearLayout>    </RelativeLayout></layout>

使用dataBinding后根布局就需要改成layout,然后在

<variable            name="user"            type="User"></variable>

声明需要绑定的数据,name类似于变量名,type就是类型。
绑定后我们在输入用户名的text属性中@{user.name}这样这个edittext就指定了显示user的name。
然后我们在activity的oncreate中使用ActivityLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.login)来代替原先的setcontentView.
binding.setUser(user);
这样user对象就和布局中声明的user绑定了。如果想支持双休绑定,只需要在@{user.name}的@后面加一个=号@={user.name},这样的话比如你在修改edittext的值的时候,activity代码中的user对象中的数据也会相应的改动。
如果想要获取控件可以使用binding.et_name方法直接获取id为et_name的控件然后使用。是不是很方便,更多功能可以自己去这个网站查看:
https://halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/
欢迎android新手加入我的新手入门群一起学习 145186993

“`

2 0
原创粉丝点击