#app#实现数据双向绑定

在这里插入图片描述

<?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>
        <import type="com.example.myapplication.vm.MainActivityViewModel" />
        <variable
                name="vm"
                type="MainActivityViewModel"/>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">


        <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:text="@{vm.username}"
                android:hint="@string/placeholder_username"
                android:ems="10"
                android:id="@+id/username"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"

                android:layout_marginTop="104dp"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp"
                android:drawablePadding="5dp"
                android:drawableLeft="@drawable/ic_account_box_black_24dp"

                 />

        <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="@string/placeholder_password"
                android:text="@{vm.password}"
                android:ems="10"
                android:id="@+id/password"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/username"
                android:layout_marginTop="32dp"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp"

                android:drawablePadding="5dp"

                android:drawableLeft="@drawable/ic_error_outline_black_24dp"


                />

        <Button
                android:text="清除"
                android:onClick="@{()->vm.clearText()}"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/btn_clear"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.498"
                app:layout_constraintTop_toBottomOf="@+id/password"
                android:layout_marginTop="40dp"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp"
                android:background="@color/colorYellow"
                android:textColor="@color/colorWhite"
                />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;
import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.vm.MainActivityViewModel;


public class MainActivity extends AppCompatActivity {
    private MainActivityViewModel vm;
    private ActivityMainBinding binding;
    // Button btnClear = null;
    // TextView username = null;
    // TextView password = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("info","on create ...");
        setContentView(R.layout.activity_main);
        vm = ViewModelProviders.of(this).get(MainActivityViewModel.class);
        binding =  DataBindingUtil.setContentView(this,R.layout.activity_main);
        //设置数据源
        binding.setVm(vm);
        binding.setLifecycleOwner(this);


    }


    @Override
    protected void onStart() {
        Log.i("start ","on start ....");
        super.onStart();
    }

    @Override
    protected void onStop() {
        Log.i("stop","on stop ....");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.i("destroy","on destroy ....");
        super.onDestroy();
    }

    /**
     * Dispatch onPause() to fragments.
     */
    @Override
    protected void onPause() {
        Log.i("pause ","on pause .....");
        super.onPause();
    }
}

package com.example.myapplication.vm;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

/**
 * @Author lyr
 * @create 2020/9/21 20:50
 */

public class MainActivityViewModel extends AndroidViewModel {
    MutableLiveData<String> username;
    MutableLiveData<String> password;

    public MutableLiveData<String> getUsername() {
        return username;
    }

    public MainActivityViewModel setUsername(MutableLiveData<String> username) {
        this.username = username;
        return this;
    }

    public MutableLiveData<String> getPassword() {
        return password;
    }

    public MainActivityViewModel setPassword(MutableLiveData<String> password) {
        this.password = password;
        return this;
    }

    public MainActivityViewModel(@NonNull Application application) {
        super(application);

        this.username = new MutableLiveData<>();
        this.password = new MutableLiveData<>();
    }

    public void appendUserName(String val) {
        username.setValue(username.getValue()+val);
    }
    public void appendPassword(String val) {
        password.setValue(password.getValue()+val);
    }

    public void clearText() {
        password.setValue("");
        username.setValue("");
    }
}

©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐 返回首页