案例1:点击后再页面上显示点击的次数
创建一个LiveData
MutableLiveData<Integer> mClickCount = new MutableLiveData<>(0);
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="点击的次数:" />
<Button
android:id="@+id/btn_click"
android:layout_marginTop="20dp"
android:text="点击一下"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
设置点击事件,并在点击后更新点击的总数
findViewById(R.id.btn_click).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击数字增加
Integer value = mClickCount.getValue();
//点击一下,增加数字
mClickCount.setValue(value+10);
}
});
观察LiveData,在收到更新后显示在页面上
//观察这个LiveData,当LiveData里面的值变化了,就会收到onChanged的回调
mClickCount.observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
count.setText("点击的次数:" + mClickCount.getValue());
}
});
案例二:EditText输入变化后,在下方同步显示输入的内容
创建LiveData
private MutableLiveData<String> mText = new MutableLiveData<>("");
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#500000ff"
android:padding="20dp"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#50ff0000"
android:padding="20dp"
android:textSize="30sp" />
</LinearLayout>
监听输入变化,并更新LiveData的值
etText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
mText.setValue(s.toString());
}
});
监听LiveData,变化后更新到页面上
mText.observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
tvText.setText(s);
}
});
案例三:同意隐私协议后才可以点击登录按钮
创建LiveData
private MutableLiveData<Boolean> mAgree = new MutableLiveData<>(false);
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/check_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="同意用户隐私协议"
android:textSize="20sp" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="登录" />
</LinearLayout>
监听checkBox的选中状态
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mAgree.setValue(isChecked);//fasls
}
});
监听LiveData,变化后同步设置按钮是否启用
mAgree.observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
btnLogin.setEnabled(aBoolean);
}
});