一、字符串拼接函數(shù)
在上一篇博客中 , 遇到 在 DataBinding 布局 中 , 向 TextView 組件設置 int 類型數(shù)據(jù)的情況會報錯 , 最終的處理方式是 將 int 類型的變量 student.age
通過 String.valueOf
函數(shù)轉(zhuǎn)為 字符串 類型 , 設置到 TextView 組件中 ;
<TextView
android:id="@+id/textView"
android:text="@{String.valueOf(student.age)}" />
此外 , 還可以 在 數(shù)據(jù)類 中定義 字符串拼接函數(shù) , 直接在 DataBinding 布局文件中 , 調(diào)用字符串拼接函數(shù) ;
class Student(var name: String, var age: Int) {
fun nameText(): String {
return "姓名 : ${name}"
}
fun ageText(): String {
return "年齡 : ${age}"
}
}
在 DataBinding 布局文件中 , 聲明 kim.hsl.databinding_demo.Student
類型的 對象 student ;
<data>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
</data>
布局文件中的組件中 ,
- 調(diào)用
@{student.nameText()}
設置"姓名 : ${name}"
字符串內(nèi)容 ; - 調(diào)用
@{student.ageText()}
設置"年齡 : ${age}"
字符串內(nèi)容 ;
布局文件代碼示例 :
<?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>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Tom"
android:text="@{student.nameText()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="18"
android:text="@{student.ageText()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
二、綁定點擊事件函數(shù)
在 DataBinding 布局中 , 如果想要為 View 組件綁定點擊事件 , 需要綁定參數(shù)為 View 類型 , 返回值 void 的函數(shù)即可 ;
在 Student 類中定義如下函數(shù) :
fun onClick(view: View): Unit {
Log.i("", "${nameText()} ${ageText()} 觸發(fā)點擊事件")
}
完整代碼如下 :
package kim.hsl.databinding_demo
import android.util.Log
import android.view.View
class Student(var name: String, var age: Int) {
fun nameText(): String {
return "姓名 : ${name}"
}
fun ageText(): String {
return "年齡 : ${age}"
}
fun onClick(view: View): Unit {
Log.i("", "${nameText()} ${ageText()} 觸發(fā)點擊事件")
}
}
在 DataBinding 中 View 組件的 android:onClick
屬性中 設置 @{student.onClick}
點擊函數(shù) ;
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Tom"
android:text="@{student.nameText()}"
android:onClick="@{student.onClick}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
點擊該組件后 , 會自動觸發(fā)事件 ;
三、DataBinding 布局中使用 import 標簽導入 Java、Kotlin 類
除了將 字符串拼接 函數(shù)定義在 傳入的 Student 對象中之外 , 還可以 定義在任意類的 靜態(tài)方法 中 ;
注意 : 只能在 DataBinding 布局中調(diào)用靜態(tài)方法 ;
<data>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
</data>
1、DataBinding 布局中使用 import 標簽導入 Java 類
首先 , 定義一個 Java 類 , 在其中定義靜態(tài)方法 , 接收一個值 , 拼接字符串 ;
package kim.hsl.databinding_demo;
public class JavaStudentUtils {
public static String nameText(String name) {
return "姓名 : " + name;
}
public static String ageText(int age) {
return "年齡 : " + age;
}
}
然后 , 在 <data>
標簽中 , 導入相應的類 , 如下面的 kim.hsl.databinding_demo.JavaStudentUtils
類 ;
<data>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
<import type="kim.hsl.databinding_demo.JavaStudentUtils" />
</data>
最后 , 在 DataBinding 布局的 View 組件中 的 android:text
屬性 , 設置 @{JavaStudentUtils.nameText(student.name)}
屬性值 , 通過調(diào)用 JavaStudentUtils.nameText 靜態(tài)方法 , 設置最終顯示hi的文本 ;
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Tom"
android:text="@{JavaStudentUtils.nameText(student.name)}"
android:onClick="@{student.onClick}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
2、DataBinding 布局中使用 import 標簽導入 Kotlin 類
首先 , 定義一個 Kotlin 類 , 在其中的 companion object 伴生對象 定義 Java 靜態(tài)方法 , 接收一個值 , 拼接字符串 ;
- 在 Kotlin 中定義 Java 靜態(tài)方法 : 需要在 companion object 伴生對象 中使用 @JvmStatic 注解修飾函數(shù) ;
- 在 Kotlin 中定義 Java 靜態(tài)成員 : 需要在 companion object 伴生對象 中使用 @JvmField 注解修飾字段 ;
- 特別注意 : 只有在 companion object 伴生對象 中 , 才能使用 @JvmStatic 和 @JvmField 注解 ;
package kim.hsl.databinding_demo
class KotlinStudentUtils {
companion object {
@JvmStatic
fun nameText(name: String): String {
return "姓名 : " + name
}
@JvmStatic
fun ageText(age: Int): String {
return "年齡 : " + age
}
}
}
然后 , 在 <data>
標簽中 , 導入相應的類 , 如下面的 kim.hsl.databinding_demo.JavaStudentUtils
類 ;
<data>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
<import type="kim.hsl.databinding_demo.KotlinStudentUtils" />
</data>
最后 , 在 DataBinding 布局的 View 組件中 的 android:text
屬性 , 設置 @{KotlinStudentUtils.nameText(student.name)}
屬性值 , 通過調(diào)用 KotlinStudentUtils.nameText 靜態(tài)方法 , 設置最終顯示hi的文本 ;
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="18"
android:text="@{KotlinStudentUtils.ageText(student.age)}"
android:onClick="@{student.onClick}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.2" />
四、完整代碼示例
1、數(shù)據(jù)類
在該數(shù)據(jù)類中 , 定義了字符串拼接函數(shù) , 點擊事件函數(shù) ;
該數(shù)據(jù)類對象需要在 DataBinding 布局文件中 , 需要通過如下方式導入 ;
<data>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
</data>
代碼示例 :
package kim.hsl.databinding_demo
import android.util.Log
import android.view.View
class Student(var name: String, var age: Int) {
fun nameText(): String {
return "姓名 : ${name}"
}
fun ageText(): String {
return "年齡 : ${age}"
}
fun onClick(view: View): Unit {
Log.i("", "${nameText()} ${ageText()} 觸發(fā)點擊事件")
}
}
2、導入的 Java 類
在 Java 類中 , 可以直接定義 靜態(tài)方法 , 在 DataBinding 布局中調(diào)用 ;
package kim.hsl.databinding_demo;
public class JavaStudentUtils {
public static String nameText(String name) {
return "姓名 : " + name;
}
public static String ageText(int age) {
return "年齡 : " + age;
}
}
3、導入的 Kotlin 類
在 Kotlin 類中 , 需要在 companion object 伴生對象 中使用 @JvmStatic 注解修飾函數(shù) , 才能定義 Java 靜態(tài)函數(shù) ;
package kim.hsl.databinding_demo
class KotlinStudentUtils {
companion object {
@JvmStatic
fun nameText(name: String): String {
return "姓名 : " + name
}
@JvmStatic
fun ageText(age: Int): String {
return "年齡 : " + age
}
}
}
4、DataBinding 布局文件
在布局文件中 , 導入 Student 對象 , 和 定義了靜態(tài)函數(shù)的類 ;
綁定點擊事件 :
android:onClick="@{student.onClick}"
拼接字符串 :文章來源:http://www.zghlxwxcb.cn/news/detail-401777.html
android:text="@{JavaStudentUtils.nameText(student.name)}"
代碼示例 :文章來源地址http://www.zghlxwxcb.cn/news/detail-401777.html
<?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>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
<import type="kim.hsl.databinding_demo.JavaStudentUtils" />
<import type="kim.hsl.databinding_demo.KotlinStudentUtils" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Tom"
android:text="@{JavaStudentUtils.nameText(student.name)}"
android:onClick="@{student.onClick}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="18"
android:text="@{KotlinStudentUtils.ageText(student.age)}"
android:onClick="@{student.onClick}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
5、Activity 組件類
package kim.hsl.databinding_demo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import kim.hsl.databinding_demo.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 設置布局文件
// 布局文件是 activity_main.xml
// 該類名稱生成規(guī)則是 布局文件名稱 + Binding
var activityMainBinding: ActivityMainBinding =
DataBindingUtil.setContentView(this, R.layout.activity_main)
// 為布局 設置 數(shù)據(jù)
activityMainBinding.student = Student("Jerry", 13)
}
}
到了這里,關于【Jetpack】DataBinding 架構(gòu)組件 ② ( 字符串拼接函數(shù) | 綁定點擊事件函數(shù) | DataBinding 布局中使用 import 標簽導入 Java、Kotlin 類 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!