개발/Android
[JAVA] 안드로이드 LiveData 초간단 예제 (ViewModel 안 쓰고)
레란희
2022. 11. 2. 16:37
이번에는 라이브 데이터만 다루는 예제이다
1. MainActivity
package com.monkey.viewmodeltest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tvNormal;
Button btnPlus, btnMinus;
MutableLiveData<Integer> normalCount = new MutableLiveData<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
normalCount.setValue(0);
tvNormal = findViewById(R.id.tv_normal);
btnPlus = findViewById(R.id.btn_plus);
btnMinus = findViewById(R.id.btn_minus);
normalCount.observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
setCountText();
}
});
btnPlus.setOnClickListener(v-> {
normalCount.setValue(normalCount.getValue()+1);
});
btnMinus.setOnClickListener(v-> {
normalCount.setValue(normalCount.getValue()-1);
});
}
@Override
protected void onResume() {
super.onResume();
setCountText();
}
private void setCountText() {
tvNormal.setText(normalCount.getValue() +"");
}
}
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
app:layout_constraintTop_toBottomOf="@id/tv_normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn_plus" />
<Button
android:id="@+id/btn_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
app:layout_constraintLeft_toRightOf="@id/btn_minus"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/btn_minus"
app:layout_constraintBottom_toBottomOf="@id/btn_minus"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NORMAL"
android:textColor="@color/purple_500"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="@id/tv_normal"
app:layout_constraintRight_toRightOf="@id/tv_normal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/tv_normal"
app:layout_constraintVertical_bias="1"/>
<TextView
android:id="@+id/tv_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#000"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
plus, minus 버튼에서 setText를 해 주지 않아도
값이 변하면 텍스트가 변하게 된다!