第一部分利用BaseObservable实现双向绑定:
UserViewModel.java:
package com.example.myjetpack08;
import android.util.Log;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
/*
@author zxk
@version 1.0
*/
public class UserViewModel extends BaseObservable {
private User user;
public UserViewModel() {
this.user = new User("Albert Einstein");
}
@Bindable
public String getUserName(){
return user.username;
}
public void setUserName(String userName){
if (userName != null && !userName.equals(user.username)){
Log.d("这是JetPack日志:","username 是:"+userName);
user.username = userName;
notifyPropertyChanged(BR.userName);
}
}
}
User.java:
package com.example.myjetpack08;
public class User {
public String username;
public User(String username) {
this.username = username;
}
}
MainActivity.java:
package com.example.myjetpack08;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import com.example.myjetpack08.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
activityMainBinding.setUserViewModel(new UserViewModel());
}
}
activity_main.xml:
<?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="userViewModel"
type="com.example.myjetpack08.UserViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@={userViewModel.userName}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.457" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
第二部分利用ObservableField实现双向绑定:
UserViewModel:
package com.example.myapplication;
import android.util.Log;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.ObservableField;
/*
@author zxk
@version 1.0
*/
public class UserViewModel {
private User user;
private ObservableField<User> userObservableField;
public UserViewModel() {
this.user = new User("Albert Einstein");
userObservableField = new ObservableField<>();
userObservableField.set(user);
}
//就是获取控件上的输入的值
public String getUserName(){
return userObservableField.get().username;
}
//修改控件上的值
public void setUserName(String userName){
Log.d("这是JetPack日志","username is "+userName);
userObservableField.get().username = userName;
}
}
User.java:
package com.example.myapplication;
public class User {
public String username;
public User(String username) {
this.username = username;
}
}
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import com.example.myapplication.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
activityMainBinding.setUserViewModel(new UserViewModel());
}
}
activity_main.xml:
<?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="userViewModel"
type="com.example.myapplication.UserViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@={userViewModel.userName}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.457" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
这两种做法,都可以,用起来最方便就是第二种(我个人想法)