当前位置: 首页 > news >正文

分阶段建设网站东莞交易中心

分阶段建设网站,东莞交易中心,绍兴网站建设报价,新增域名网站建设方案1.ViewBinding:视图绑定 通过视图绑定功能,您可以更轻松地编写可与视图交互的代码。在模块中启用视图绑定之后,系统会为该模块中的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。在大多数情况下&…

1.ViewBinding:视图绑定

通过视图绑定功能,您可以更轻松地编写可与视图交互的代码。在模块中启用视图绑定之后,系统会为该模块中的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。在大多数情况下,视图绑定会替代 findViewById。

开启ViewBinding

img

为某个模块启用视图绑定功能后,系统会为该模块中包含的每个 XML 布局文件生成一个绑定类。每个绑定类均包含对根视图以及具有 ID 的所有视图的引用。系统会通过以下方式生成绑定类的名称:将 XML 文件的名称转换为驼峰式大小写,并在末尾添加“Binding”一词。

public class MainActivity extends AppCompatActivity {ActivityMainBinding activityMainBinding;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 初始化  --> 加载布局文件activityMainBinding=ActivityMainBinding.inflate(getLayoutInflater());setContentView(activityMainBinding.getRoot());//       获取对于的按键activityMainBinding.button.setText("Hello World");}
}

img

DataBinding:数据绑定库

可以使用声明性格式(而非程序化地)将布局中的界面组件绑定到应用中的数据源。是实现 view 和 data 绑定的工具,把数据映射到 view 的 xml中,可以在 xml 布局文件中实现 view 的赋值,方法调用。使用 DataBinding 后,我们不用再写 findViewById,不用再获取控件对象,不用再设置监听,可以节省我们 activity 中的很多获取控件,赋值,添加监听所需要的代码。

  1. 开启databinding
    img
  2. 修改布局文件
    选中布局文件的第一行,按alter+enter就会弹出提示,默认选中data binding layout
    imgimg
  3. 如果您要在 Fragment、ListView 或 RecyclerView 适配器中使用数据绑定项,您可能更愿意使用绑定类或 DataBindingUtil 类的 inflate() 方法,如以下代码示例所示:
    ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false);// orListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);
  1. 属性绑定:
    data 中的 user 变量描述了可在此布局中使用的属性。
    <variable name="user" type="com.example.User" />
    布局中的表达式使用“@{}”语法写入特性属性中。在这里,TextView 文本被设置为 user 变量的 firstName 属性:
<TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@{user.firstName}" />

img

img

会生成对应的getter与setter方法

复杂数据的绑定

  1. 定义复杂数据
    img
  2. 绑定数据
<?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><variablename="person"type="com.hnucm.databinding.Person" /></data><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@{person.name}"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@{person.like}"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textView" /></androidx.constraintlayout.widget.ConstraintLayout>
</layout>
  1. 在代码中赋初值
Person person = new Person();
person.name = "hu";
person.like = "nana";
activityMainBinding.setPerson(person);

img

  1. 事件绑定

    1. databinding可以把事假当做数据绑定到内部文件中
    2. img
    3. img
    4. activityMainBinding.setMyclick(new MyClick());
      5img 6. 点击事件回传数据
public void onClick(Person person){Log.i("success",person.like+person.name);
}----
android:onClick="@{()->myclick.onClick(person)}"
http://www.yayakq.cn/news/462360/

相关文章:

  • 西昌做网站如何将自己做的网站
  • 十佳网站设计江西网站建设平台
  • 网站开发建设推荐色系网站哪里有
  • 什么网站做淘宝素材比较好模板网免费下载素材
  • 做网站不给源码超级seo外链
  • 建好的网站能修改吗wordpress自带轮播
  • 番禺建设网站多少钱让人做网站 需要准备什么
  • dedecms制作的网站椒江做阿里巴巴网站的公司
  • 赣州找工作的网站ppt可爱模板免费下载
  • 年栾洪全单页做网站教程塘厦做网站
  • seo在线网站推广配置wordpress环境
  • 下载官方网站app下载免费主题网站
  • 东莞建设网站开发中国建筑网官网查询系统平台
  • 网站开发的主要工作步骤数据库支持的网站怎么做
  • 课程网站建设课程自己做网站怎么赢利
  • 中国建设银行官网站企业银行wordpress 插件里有中文
  • 咖啡网站源码公司介绍文案
  • vs网站开发如何发布网站这么做
  • 怎么看公司网站是哪里做的在线作图网站
  • 拉米拉云网站建设哪里可以注册免费域名
  • window7用jsp做的网站要什么工具网站伪静态怎么做
  • 虚拟云服务做网站WordPress开启meme
  • 建站平台选择建议怎么做跨境电商开店
  • 南通住房和城乡建设厅网站首页凡客精选带货达人
  • 咸阳企业做网站电脑做系统哪个网站比较好
  • 网站开发 设计文档网店如何推广自己的产品
  • 网站建设内容保障工作个人总结微信小程序开发环境搭建
  • 重庆好的推广网站手机网站推荐导航页
  • 营销建设网站网站如何做京东联盟
  • 做感恩网站的图片做网站怎么提取视频无广告