android,jetpack利用BaseObservable和ObservableField分别实现双向绑定

第一部分利用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>

这两种做法,都可以,用起来最方便就是第二种(我个人想法)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值