Jetpack架构组件_4. 数据绑定库页面传递数据

news2024/10/6 14:29:39

        本篇介绍数据源从activity_main(1级页面)传递给include布局(2级页面)。

1.实现步骤

      step1.修改build.gradle文件

        修改app模块下的build.gradle文件,增加如下内容:

    dataBinding {
        enabled = true
    }

         step2.创建模型类User类。

package com.gaoting.databindingtosecondpage;

public class User {
    public String userName;
    public String password;
}

         step3.修改activity_main.xml 布局文件

         在<data>标签页下面创建变量user。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="user"
            type="com.gaoting.databindingtosecondpage.User" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:layout_margin="30dp"
            android:text="我是1级页面"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_margin="30dp"
            android:text="@{user.userName}"
            android:id="@+id/edtUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入用户名!" />

        <EditText
            android:layout_margin="30dp"
            android:text="@{user.password}"
            android:id="@+id/edtPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入密码!" />

    </LinearLayout>
</layout>

         在MainActivity中使用ActivityMainBingding对象就可以操控UI控件。

package com.gaoting.databindingtosecondpage;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;

import com.gaoting.databindingtosecondpage.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding activityMainBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        User user = new User();
        user.userName="gaoting";
        user.password="123456";
        activityMainBinding.setUser(user);
    }
}

         step4.创建second_include.xml布局文件

        创建second_include.xml布局文件,把XML布局文件转换为DataBinding可以识别和绑定的布局文件。(选中根节点LinearLayout,按Alt+Enter弹出快捷菜单Convert to data binding layout。),在<data>节点下要引入变量user,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="user"
            type="com.gaoting.databindingtosecondpage.User" />
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_margin="30dp"
            android:text="我是二级页面"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:layout_margin="30dp"
            android:text="@{user.userName}"
            android:id="@+id/edtUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入用户名!" />

        <EditText
            android:layout_margin="30dp"
            android:text="@{user.password}"
            android:id="@+id/edtPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入密码!" />

    </LinearLayout>
</layout>

         step5.关键步骤,引入app命名空间

        引入app命名空间xmlns:app="http://schemas.android.com/apk/res-auto",然后把1级界面定义的变量传递给2级页面。方法如下:

        <include layout="@layout/second_include" app:user="@{user}">  

         【注】include的用法:在开发Android布局时,我们常将一些通用的视图提取到一个单独的layout文件中,然后使用标签在需要使用的其他layout布局文件中加载进来,比如我们自己App导航栏等。这样,便于对相同视图内容进行统一的控制管理,提高布局重用性。 

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
        <include layout="@layout/second_include"
            app:user="@{user}">

        </include>

2.代码示例

        完整的代码如下:

        1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="user"
            type="com.gaoting.databindingtosecondpage.User" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
        <include layout="@layout/second_include"
            app:user="@{user}">

        </include>

        <TextView
            android:layout_margin="30dp"
            android:text="我是1级页面"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_margin="30dp"
            android:text="@{user.userName}"
            android:id="@+id/edtUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入用户名!" />

        <EditText
            android:layout_margin="30dp"
            android:text="@{user.password}"
            android:id="@+id/edtPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入密码!" />

    </LinearLayout>
</layout>

        2)second_include.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="user"
            type="com.gaoting.databindingtosecondpage.User" />
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_margin="30dp"
            android:text="我是二级页面"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:layout_margin="30dp"
            android:text="@{user.userName}"
            android:id="@+id/edtUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入用户名!" />

        <EditText
            android:layout_margin="30dp"
            android:text="@{user.password}"
            android:id="@+id/edtPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入密码!" />

    </LinearLayout>
</layout>

        3)MainActivity.java

package com.gaoting.databindingtosecondpage;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;

import com.gaoting.databindingtosecondpage.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding activityMainBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        User user = new User();
        user.userName="gaoting";
        user.password="123456";
        activityMainBinding.setUser(user);
    }
}

        4)User.java

package com.gaoting.databindingtosecondpage;

public class User {
    public String userName;
    public String password;
}
        UI控件:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1714131.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

WPF之TextBlock文本标签

TextBlock: 用于显示文本内容 常用属性 Text设置展示的文本fontsize设置字体大小FontWeight设置字体粗细FontFamily设置字体样式 实例 <Grid><TextBlock Text"显示文本"FontSize"10"FontWeight"Bold"Foreground"red">&l…

【机器学习300问】105、计算机视觉(CV)领域有哪些子任务?

计算机视觉作为人工智能的重要分支&#xff0c;发展至今已经在诸多领域取得显著的成果。在众多的计算机视觉任务中&#xff0c;图像分类、目标检测与定位、语义分割和实例分割是四个基本而关键的子任务&#xff0c;它们在不同的应用场景下扮演着重要角色。这四个子任务虽然各具…

Neovim 配置全面解析(下)

Neovim 配置全面解析&#xff08;下&#xff09; 原文&#xff1a;Neovim 配置全面解析&#xff08;下&#xff09; - 知乎 (zhihu.com) 环境&#xff1a;Ubuntu 20.04 宿主机&#xff1a;windows &#xff08;windows terminal&#xff09;WSL 2 NVIM&#xff1a;v 0.10.0-de…

是如何学习 Java 的?

我曾在携程旅行网做 Java 开发&#xff0c;也曾拿过阿里 P7 offer 和饿了么、美团等公司的 offer&#xff0c;这是职位都是 Java 开发岗&#xff0c;也做过 Java 面试官面试过不少同学。下面我就和大家分享一下我学习 Java的经验。 我将从 Java 基础知识、Java 框架、计算机基…

Java设计模式 _行为型模式_备忘录模式

一、备忘录模式 1、备忘录模式 备忘录模式&#xff08;Memento Pattern&#xff09;是一种行为型模式。通过保存一个对象的某个状态&#xff0c;以便在适当的时候恢复对象。 2、实现思路 &#xff08;1&#xff09;、定义记录数据的格式规范。 &#xff08;2&#xff09;、编…

计算机算法中的数字表示法——原码、反码、补码

目录 1.前言2.研究数字表示法的意义3.数字表示法3.1 无符号整数3.2 有符号数值3.3 二进制补码(Twos Complement, 2C)3.4 二进制反码(也称作 1 的补码, Ones Complement, 1C)3.5 减 1 表示法(Diminished one System, D1)3.6 原码、反码、补码总结 1.前言 昨天有粉丝让我讲解下定…

SRE视角下的DevOps构建之道

引言&#xff1a; 随着数字化时代的飞速发展&#xff0c;软件成为了企业竞争力的核心。为了更高效地交付高质量的软件&#xff0c;DevOps&#xff08;Development和Operations的组合&#xff09;作为一种文化、实践和工具集的集合&#xff0c;逐渐成为了行业内的热门话题。然而…

怎样快速查找网页代码中存在的错误?

计算机很机械&#xff0c;代码中存在微小的错误&#xff0c;计算机就得不到正确的运行结果。比如&#xff0c;一个字母的大小写、比如&#xff0c;个别地方丢掉了一个符号、、、如此等等。这就要求程序员和计算机是心灵相通的&#xff0c;不能有任何的“隔阂”。 但是&#xf…

汇智知了堂实力展示:四川农业大学Python爬虫实训圆满结束

近日&#xff0c;汇智知了堂在四川农业大学举办的为期五天的校内综合项目实训活动已圆满结束。本次实训聚焦Python爬虫技术&#xff0c;旨在提升学生的编程能力和数据分析能力&#xff0c;为学生未来的职业发展打下坚实的基础。 作为一家在IT教育行业享有盛誉的机构&#xff…

【ArcGISPro】3.1.5下载和安装教程

下载教程 arcgis下载地址&#xff1a;Трекер (rutracker.net) 点击磁力链下载弹出对应的软件进行下载 ArcGISPro3.1新特性 ArcGIS Pro 3.1是ArcGIS Pro的最新版本&#xff0c;它引入了一些新的特性和功能&#xff0c;以提高用户的工作效率和数据分析能力。以下是ArcGIS…

基于Udp(收发信息使用同一个socket)网络通信编程

想要实现网络通信那么就要有一个客户端一个服务器 客户端发送数据&#xff0c;服务器接收数据并返回数据 网络通信就是进程通信 所以我们用两个程序来分别编写客户端和服务器 服务器 1&#xff0c;设置端口号&#xff0c; 2、ip可以固定位127.0.0.1来用于本地测试&#xff0c…

dbserver 软件 展示 全部模式库

目录 1 问题2 实现 1 问题 dbserver 软件 展示 全部模式库 2 实现 以上就可以了

基于文本来推荐相似酒店

基于文本来推荐相似酒店 查看数据集基本信息 import pandas as pd import numpy as np from nltk.corpus import stopwords from sklearn.metrics.pairwise import linear_kernel from sklearn.feature_extraction.text import CountVectorizer from sklearn.feature_extrac…

扩散模型--论文分享篇

定义&#xff1a;输入文本与图像&#xff0c;生成对图像的描述。 所采用的方法&#xff1a;对比学习、基于跨注意力机制的多模态融合 基于扩散模型的方法&#xff1a;主要介绍的扩散的原理 图像生成任务介绍 GAN VAE 扩散模型 基于GAN的图像生成&#xff0c;一个生成器与判别…

非量表题如何进行信效度分析

效度是指设计的题确实在测量某个东西&#xff0c;一般问卷中使用到。如果是量表类的数据&#xff0c;其一般是用因子分析这种方法去验证效度水平&#xff0c;其可通过因子分析探究各测量量表的内部结构情况&#xff0c;分析因子分析得到的内部结构与自己预期的内部结构进行对比…

子网划分案例

4.2子网划分 “有类编址”的地址划分过于死板&#xff0c;划分的颗粒度太大&#xff0c;会有大量的主机号不能被充分利用&#xff0c;从而造成了大量的IP地址资源浪费。因此可以利用子网划分来减少地址浪费&#xff0c;即VLSM (Variable Length Subnet Mask)&#xff0c;可变长…

Java实现对象存储的4种方式(本地对象存储、MINIO、阿里云OSS、FastDFS)

文章目录 Java实现对象存储的3中方式1、概述2、本地对象存储2.1 配置本地文件相关信息2.2 通用映射配置 ResourcesConfig2.3 文件上传业务 LocalSysFileServiceImpl2.4 上传接口2.5 演示 3、MINIO3.1 依赖3.2 配置3.3 配置连接信息3.4. MINIO文件上传业务3.5 文件上传下载接口3…

高考前很焦虑?看看罗永浩提的三个建议!罗永浩推荐的随身WiFi居然蕴含这样的商机?2024普通人如何翻身?

你能相信现如今身家过亿的老罗罗永浩高中就辍学了吗&#xff1f;相信很多人都不敢置信吧。罗永浩无论是表现出来的口才、情商还是智商&#xff0c;无论如何都无法让人把他和高中辍学联系起来。 而这一点似乎也是老罗人生中的一个遗憾&#xff0c;于是又在一年高考季的时候&…

【AREngine BUG 解决方法】无法获取有效的相机图像尺寸

近期拿了一台 华为mate20 Pro的手机&#xff0c;在运行AR示例的过程中出现了黑屏。 问题排查 SDK版本&#xff1a;com.huawei.hms:arenginesdk:3.7.0.3 定位 经排查&#xff0c;发现(ARCamera对象的相机内参) getImageDimensions()返回的图像尺寸的width和height都为0。 这…

【AI大模型】如何让大模型变得更聪明?基于时代背景的思考

【AI大模型】如何让大模型变得更聪明 前言 在以前&#xff0c;AI和大模型实际上界限较为清晰。但是随着人工智能技术的不断发展&#xff0c;基于大规模预训练模型的应用在基于AI人工智能的技术支持和帮助上&#xff0c;多个领域展现出了前所未有的能力。无论是自然语言处理、…