布局和绑定表达式

借助表达式语言,可以编写表达式来处理视图分派的事件。数据绑定库会自动生成讲布局中的视图与您的数据对象绑定所需的类。

数据绑定文件有所不同,已根据layout开头,后跟data元素和view根元素。此视图元素是非绑定布局文件中的根。以下代码展示了实例布局文件:

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <import type="java.util.List"/>
        <import type="java.util.Map"/>
        <variable
            name="user"
            type="com.bw.day1110.User" />
        <variable
            name="view"
            type="android.view.View" />
        <variable
            name="list"
            type="List&lt;String>" />
        <variable
            name="index"
            type="int" />
        <variable
            name="map"
            type="Map&lt;String ,String>" />
        <variable
            name="key"
            type="String" />
        <variable
            name="presenter"
            type="com.bw.day1110.Presenter" />
        <variable
            name="userinfo"
            type="com.bw.day1110.Userinfo" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity2">
        <TextView
            android:text="@{user.name}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(user.age)}"
            android:visibility="@{user.age>19? view.GONE:view.VISIBLE}"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/input_et"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/input_tv"
            android:text="@{inputEt.text}"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{list.get(index)}"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{map.get(key)}"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="事件绑定"
            android:onClick="@{()->presenter.onClick()}"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{userinfo.name}"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{presenter.firstName}"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="可观察数据对象"
            android:onClick="@{()->presenter.onSaveClick(userinfo)}"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/iv"/>
    </LinearLayout>

</layout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值