Ram*_*ama 6 data-binding android bidirectional 2-way-object-databinding android-databinding
我正在使用数据绑定,我创建了一个非常简单的类
public class ViewUser extends BaseObservable {
private String name;
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
}
Run Code Online (Sandbox Code Playgroud)
布局简单
<?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="user"
type="com.example.ViewUser" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:lines="3"
android:text="@{user.name}" />
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
当我更新对象时,UI更新没有任何问题,但是如果我从UI更改EditText的值然后让用户使用DataBindingUtil .getUser(),则它没有更新的值.是否可以自动更新属性,或者是否必须使用TextWatcher的onTextChanged等事件更新对象?
小智 11
在以下情况之后,您的xml标记android:text缺少a :
=@
android:text="@={user.name}"
的@={}意思是"双向数据绑定".这@{}是一种数据绑定方式.
| 归档时间: |
|
| 查看次数: |
6053 次 |
| 最近记录: |