前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >How to use DataBinding?

How to use DataBinding?

作者头像
ppjun
发布2018-09-05 11:55:47
4810
发布2018-09-05 11:55:47
举报
文章被收录于专栏:ppjun专栏ppjun专栏

A : How to use DataBinding? B : such as read this circle.

Open setting

First , open your file which is in app/build.gradle , and you can see the android' field , you need to write the code like this to open the dataBinding' Setting.

代码语言:javascript
复制
android {
    ...
  dataBinding {
    enabled = true
 }
}

Create a bean

Second , create a bean which is named User in your package , and create two variable as well as generate their constroctor. Two variable , name and age , were String variable and int variable . you must use the public to decorate two variable ,otherwise it wiil catch the Exception such as like this :

代码语言:javascript
复制
Could not find accessor package.User.name

here are the full code

代码语言:javascript
复制
public class User {
    public String name;
    public String age;
   
   public User(String name, String age) {
        this.name = name;
        this.age = age;
    }
}

Modify the xml

Thrid , add the <layout> label to root in your xml . by the way ,you need to write the android nameSpace in the layout element. create the <data> label which in the <layout> label . create the <import> label to import the User class which created juts now . create the <variable> label next to the <import> with two property which is called name and type. you can write the name which just like user, and type must be call User .

Now , you can give the text to TextView by Using anroid:text="@{user.name}"

here are the full code:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<layout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"ja>

<data>
    <import type="com.ppjun.android.databinding.User"/>
    
    <variable
        name="user"
        type="User"/>
    
</data>


<RelativeLayout

    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


   <TextView
        android:layout_below="@+id/toolbar"
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{user.name}"
       />

    <TextView
        android:id="@+id/age"
        android:layout_below="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{user.age}"/>


</RelativeLayout>
</layout>

Connect to the data

Final , gradle sync to generate the AcrivityMainBingding .

In the MainActivity 'onCreate method , create the AcrivityMainBingding binding by Using the method DataBindingUtil.setCotnentView(this,R.layout.activity_main);

new the User variable by it's constroctor.

user binding.setUser(user); to binding the xml.

here are the full code:

代码语言:javascript
复制
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User("ppjun", "23");
binding.setUser(user);

run the application you will see the TextView text like ppjun and 23.

Summary

  1. DataBinding is easy for us to set the text without findviewbyid.
  2. The next article will show you more things about DataBinding.

Thanks for your watching. :)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Open setting
  • Create a bean
  • Modify the xml
  • Connect to the data
  • Summary
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档