Android 安卓DataBinding(八)·引用类方法


前景回顾

Android 安卓DataBinding(一)·基础
Android 安卓DataBinding(二)·入门
Android 安卓DataBinding(三)·单向绑定 BaseObservable
Android 安卓DataBinding(四)·单向绑定 ObservableField
Android 安卓DataBinding(五)·单向绑定 ObservableCollection(集合使用)
Android 安卓DataBinding(六)·双向绑定
Android 安卓DataBinding(七)·事件绑定


引用类中的方法

这个最简单了,非常好理解,所以这篇文章这么短,也就不给效果图了
首先要定义一个静态方法:注意,里面一定要使用static哦!之后用

package com.lkdot.data;

public class GoodFunction {
    public static String thisFunction(String str) {
        return str + " hello world";

    }
}


之后可以在布局中使用 import 标签引入进来即可(不要再用variable变标签了,否则会报错的),直接食用GoodFunction.方法就行。

android:text="@{GoodFunction.thisFunction(data.username)}"

<?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="com.lkdot.data.DataBean" />

        <import type="com.lkdot.data.GoodFunction" />


        <variable
            name="data"
            type="DataBean" />

    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginTop="8dp"
            android:gravity="left|center"
            android:paddingLeft="20dp"
            android:text="username:"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="#03A9F4"
            android:gravity="left|center"
            android:paddingLeft="20dp"
            android:text="@{GoodFunction.thisFunction(data.username)}"
            android:textColor="#FFFFFF"
            android:textSize="14dp"
            android:textStyle="bold"
            app:layout_constraintTop_toBottomOf="@+id/textView4"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="0dp" />

    </android.support.constraint.ConstraintLayout>
</layout>

下一篇文章 Android 安卓DataBinding(九)·运算符



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

第三女神程忆难

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值