示例图:
1.导包 在build.gradle 中 加入
// 使用androidx版本库
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha03'
2. 开启dataBinding
android{
...
// 步骤1.开启data binding
dataBinding {
enabled = true
}
...
}
3.写个类继承 ViewModel
package com.example.mydatabindingtest;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class MyViewModel extends ViewModel {
private MutableLiveData<Integer> redNumb;
private MutableLiveData<Integer> greenNumb;
//回滚 用于记录前面一次的值
private int redBack,greenBack;
public MutableLiveData<Integer> getRedNumb() {
if (redNumb == null) {
redNumb = new MutableLiveData<>();
redNumb.setValue(0);
}
return redNumb;
}
public MutableLiveData<Integer> getGreenNumb() {
if (greenNumb == null) {
greenNumb = new MutableLiveData<>();
greenNumb.setValue(0);
}
return greenNumb;
}
public void redAdd(int num) {
//添加之前保留之前的值 都记录下
redBack = redNumb.getValue();
greenBack = greenNumb.getValue();
redNumb.setValue(getRedNumb().getValue() + num);
}
public void greenAdd(int num) {
//添加之前保留之前的值 都记录下
redBack = redNumb.getValue();
greenBack = greenNumb.getValue();
greenNumb.setValue(getGreenNumb().getValue() + num);
}
//重置
public void reset(){
//添加之前保留之前的值
greenBack = greenNumb.getValue();
redBack = redNumb.getValue();
redNumb.setValue(0);
greenNumb.setValue(0);
}
//撤销一次
public void rollBack(){
//把记录的值设置到变量中
redNumb.setValue(redBack);
greenNumb.setValue(greenBack);
}
}
4.更改布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 步骤2. 点击小灯泡生成新的xml 或者自己更改 -->
<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="data"
type="com.example.mydatabindingtest.MyViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/btn_add1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.45" />
<Button
android:id="@+id/btn_green_add3"
style="@style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00FFE7"
android:onClick="@{() -> data.greenAdd(3)}"
android:scrollbarThumbHorizontal="@null"
android:scrollbarThumbVertical="@null"
android:text="@string/text_add3"
android:textColor="#0288D1"
android:textColorHighlight="@null"
android:textColorHint="@null"
android:textColorLink="@null"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline16"
app:layout_constraintEnd_toStartOf="@+id/guideline19"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toTopOf="@+id/guideline15"
tools:ignore="DuplicateSpeakableTextCheck,VisualLintButtonSize" />
<Button
android:id="@+id/btn_red_add1"
style="@style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFff0000"
android:onClick="@{() -> data.redAdd(1)}"
android:scrollbarThumbHorizontal="@null"
android:scrollbarThumbVertical="@null"
android:text="@string/text_add1"
android:textColor="@color/white"
android:textColorHighlight="@null"
android:textColorHint="@null"
android:textColorLink="@null"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline14"
app:layout_constraintEnd_toStartOf="@+id/guideline22"
app:layout_constraintStart_toStartOf="@+id/guideline18"
app:layout_constraintTop_toTopOf="@+id/guideline13"
app:layout_constraintVertical_bias="0.52"
tools:ignore="DuplicateSpeakableTextCheck,VisualLintButtonSize" />
<Button
android:id="@+id/btn_green_add1"
style="@style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00FFE7"
android:onClick="@{() -> data.greenAdd(1)}"
android:scrollbarThumbHorizontal="@null"
android:scrollbarThumbVertical="@null"
android:text="@string/text_add1"
android:textColor="#0288D1"
android:textColorHighlight="@null"
android:textColorHint="@null"
android:textColorLink="@null"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline14"
app:layout_constraintEnd_toStartOf="@+id/guideline19"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toTopOf="@+id/guideline13"
tools:ignore="VisualLintButtonSize" />
<Button
android:id="@+id/btn_green_add2"
style="@style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00FFE7"
android:onClick="@{() -> data.greenAdd(2)}"
android:scrollbarThumbHorizontal="@null"
android:scrollbarThumbVertical="@null"
android:text="@string/text_add2"
android:textColor="#0288D1"
android:textColorHighlight="@null"
android:textColorHint="@null"
android:textColorLink="@null"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline15"
app:layout_constraintEnd_toStartOf="@+id/guideline19"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toTopOf="@+id/guideline14"
tools:ignore="DuplicateSpeakableTextCheck,VisualLintButtonSize" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.55" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.05" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1504788" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.35" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.45" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.55" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.65" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75" />
<TextView
android:textColor="@color/white"
android:gravity="center"
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFff0000"
android:text="@string/text_A"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/guideline11"
app:layout_constraintEnd_toStartOf="@+id/guideline22"
app:layout_constraintStart_toStartOf="@+id/guideline18"
app:layout_constraintTop_toTopOf="@+id/guideline10" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00FFE7"
android:gravity="center"
android:text="@string/text_B"
android:textColor="#0288D1"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/guideline11"
app:layout_constraintEnd_toStartOf="@+id/guideline19"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toTopOf="@+id/guideline10" />
<TextView
android:id="@+id/text_red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@{String.valueOf(data.redNumb)}"
android:textColor="@color/btn_bg"
android:textSize="50sp"
app:layout_constraintBottom_toTopOf="@+id/guideline13"
app:layout_constraintEnd_toStartOf="@+id/guideline22"
app:layout_constraintStart_toStartOf="@+id/guideline18"
app:layout_constraintTop_toTopOf="@+id/guideline11"
app:layout_constraintVertical_bias="0.526" />
<TextView
android:id="@+id/text_green"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center"
android:text="@{String.valueOf(data.greenNumb)}"
android:textColor="#0288D1"
android:textColorHighlight="@null"
android:textColorHint="@null"
android:textColorLink="@null"
android:textSize="50sp"
app:layout_constraintBottom_toTopOf="@+id/guideline13"
app:layout_constraintEnd_toStartOf="@+id/guideline19"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toTopOf="@+id/guideline11" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.05" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.95" />
<ImageView
android:id="@+id/img_rollback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/img_rollback_alt"
android:onClick="@{() -> data.rollBack()}"
app:layout_constraintBottom_toTopOf="@+id/guideline17"
app:layout_constraintEnd_toStartOf="@+id/guideline9"
app:layout_constraintHorizontal_bias="0.38"
app:layout_constraintStart_toStartOf="@+id/guideline18"
app:layout_constraintTop_toTopOf="@+id/guideline16"
app:layout_constraintVertical_bias="0.166"
app:srcCompat="@drawable/baseline_reply" />
<ImageView
android:id="@+id/img_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/img_reset_alt"
android:onClick="@{() -> data.reset()}"
app:layout_constraintBottom_toTopOf="@+id/guideline17"
app:layout_constraintEnd_toStartOf="@+id/guideline19"
app:layout_constraintHorizontal_bias="0.521"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toTopOf="@+id/guideline16"
app:layout_constraintVertical_bias="0.166"
app:srcCompat="@drawable/baseline_cached_24" />
<Button
android:id="@+id/btn_red_add2"
style="@style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFff0000"
android:onClick="@{() -> data.redAdd(2)}"
android:scrollbarThumbHorizontal="@null"
android:scrollbarThumbVertical="@null"
android:text="@string/text_add2"
android:textColor="@color/white"
android:textColorHighlight="@null"
android:textColorHint="@null"
android:textColorLink="@null"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline15"
app:layout_constraintEnd_toStartOf="@+id/guideline22"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline18"
app:layout_constraintTop_toTopOf="@+id/guideline14"
app:layout_constraintVertical_bias="0.48"
tools:ignore="VisualLintButtonSize" />
<Button
android:id="@+id/btn_red_add3"
style="@style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFff0000"
android:onClick="@{() -> data.redAdd(3)}"
android:scrollbarThumbHorizontal="@null"
android:scrollbarThumbVertical="@null"
android:text="@string/text_add3"
android:textColor="@color/white"
android:textColorHighlight="@null"
android:textColorHint="@null"
android:textColorLink="@null"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline16"
app:layout_constraintEnd_toStartOf="@+id/guideline22"
app:layout_constraintStart_toStartOf="@+id/guideline18"
app:layout_constraintTop_toTopOf="@+id/guideline15"
app:layout_constraintVertical_bias="0.56"
tools:ignore="VisualLintButtonSize" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
5.MainActivity.java
package com.example.mydatabindingtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;
import com.example.mydatabindingtest.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
MyViewModel myViewModel;
ActivityMainBinding binding;
private long exitTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
binding.setData(myViewModel);
binding.setLifecycleOwner(this);
}
//返回
@Override
public void onBackPressed() {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
super.onBackPressed();
}
}
}