通过学习数据绑定(bind)
,我创建了一个简单的 Activity 来开始锻炼。
并具有以下代码:
主要 Activity :
class MainActivity : AppCompatActivity() {
val users = UserProfile("First Name","Last Name","Off",false)
val handlerbtn = HandlerButtonClick()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main)
binding.user = users
binding.handler = handlerbtn
}
}
点击类:
class HandlerButtonClick {
fun onClickButton(view: View, user: UserProfile) {
var msg = ""
if(!user.isOnline){
msg = "Online"
user.firstName = "Henrique"
user.lastName = "Mendes"
user.status = "Online"
user.isOnline = true
}else{
msg = "Offline"
user.firstName = "First Name"
user.lastName = "Last Name"
user.status = "Offline"
user.isOnline = false
}
Toast.makeText(view.context, msg, Toast.LENGTH_SHORT).show()
}
}
用户配置文件类:
class UserProfile {
var firstName: String = ""
var lastName: String = ""
var status: String = ""
var isOnline: Boolean = false
constructor(firstNames: String, lastNames: String, statuss: String, isOnlines: Boolean) {
this.firstName = firstNames
this.lastName = lastNames
this.status = statuss
this.isOnline = isOnlines
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="com.dev.bob.testdatabinding2.UserProfile" />
<variable
name="handler"
type="com.dev.bob.testdatabinding2.HandlerButtonClick" />
</data>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dev.bob.testdatabinding2.MainActivity">
<View
android:layout_width="match_parent"
android:layout_height="1.0dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@{user.isOnline?@color/colorGreen : @color/colorAccent}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@{user.firstName}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@{user.lastName}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@{user.status}" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{(view)-> handler.onClickButton(view, user)}"/>
</LinearLayout>
</layout>
toast 工作正常,即变量msg
改变了。但我在屏幕上的文字没有改变。我应该怎么做才能使它们根据每个条件(If
/else
)中的内容而改变?
我的意思是,通过单击按钮,firstName
不会(再次)更改为 Henry 或 First Name
最佳答案
它的行为方式正确。来自document :
Any plain old Java object (POJO) may be used for data binding, but modifying a POJO will not cause the UI to update. The real power of data binding can be used by giving your data objects the ability to notify when data changes. There are three different data change notification mechanisms, Observable objects, observable fields, and observable collections.
或者您可以使用 this 尝试双向数据绑定(bind)快速而肮脏的方法:
您的标签 android:text
在 @:
之后缺少一个 =
。它应该是 android:text="@={user.firstName}"
。 @={}
表示“双向数据绑定(bind)”。 @{}
是一种数据绑定(bind)方式。
例如:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@={user.firstName}" />
关于android - 为什么文本在 Activity 中没有变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47773118/