<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity
>
上面这个不及时删除,就会报错:system_server E pkgName: com.example.databinding has no permission
-->
<!-- Data 标签,声明ViewModel -->
name="viewModel"
type="com.example.databinding.UserViewModel" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:text="@={viewModel.userName}" /> <!-- 双向绑定 -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄"
android:inputType="number"
android:text="@={viewModel.userAge}" /> <!-- 双向绑定 -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="实时预览:" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{`姓名: ` + viewModel.userName + `, 年龄: ` + viewModel.userAge}" />
<!-- 直接展示绑定数据 -->
</layout>