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(九)·运算符



发布了99 篇原创文章 · 获赞 1020 · 访问量 76万+

猜你喜欢

转载自blog.csdn.net/qq_40881680/article/details/102109527