How to use DataBinding?

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.

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 :

Could not find accessor package.User.name

here are the full code

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:

<?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:

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. :)

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏编程坑太多

JAVA实现Excel导入导出

4163
来自专栏lgp20151222

@Controller和@RestController的区别

我们一般都会在Controller上加上@Controller注解,但是有时候也见有加上@RestController的,现在就说说他们的区别:

1952
来自专栏一个会写诗的程序员的博客

Springboot使用JPA操作数据库第七章 使用JPA操作数据库

本章主要介绍如何在Spring Boot的Web应用中使用Mysq数据库,也充分展示Spring Boot的优势(尽可能少的代码和配置).

1723
来自专栏小筱月

controller层中,参数的获取方式以及作用域的问题

1543
来自专栏JackieZheng

Spring Boot系列——Spring Boot如何启动

​上篇《Spring Boot系列——5分钟构建一个应用》介绍了如何快速创建一个Spring Boot项目并运行。虽然步骤少流程简单,为开发者省去了很多重复性的...

963
来自专栏CodingBlock

小白也能看懂的插件化DroidPlugin原理(三)-- 如何拦截startActivity方法

前言:在前两篇文章中分别介绍了动态代理、反射机制和Hook机制,如果对这些还不太了解的童鞋建议先去参考一下前两篇文章。经过了前面两篇文章的铺垫,终于可以玩点真...

2826
来自专栏Netkiller

Spring @RequestBody 传递 List/Map 参数

本文节选自电子书《Netkiller Java 手札》 6.1.4. @RequestBody 处理 raw 原始数据,例如提交的时 application/j...

2K7
来自专栏pangguoming

在Spring MVC中使用注解的方式校验RequestParams

2314
来自专栏java、Spring、技术分享

Feign与Spring Cloud源码解析

  在Feign的官方文档上, 我们可以看到Feign最重要的一句话是:Feign makes writing java http clients easier...

5352
来自专栏pangguoming

springboot的Web开发-Web相关配置

一:Spring Boot提供自动配置        通过查看WebMvcAutoConfiguration及WebMvcProperties的源码,可以发现S...

1.2K8

扫码关注云+社区

领取腾讯云代金券