本来想弄个游戏开始的倒计时,结果用普通方法没弄出来,后来发现需要用LifeCirle可以对页面进行实时更新。于是找了相关教材。
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iniComponent();
}
public void iniComponent(){
TimerViewModel timerViewModel=new ViewModelProvider(this).get(TimerViewModel.class);
timerViewModel.setOnTimeChangeListener(new TimerViewModel.OnTimeChangeListener() {
@Override
public void onTimeChanged(int second) {
runOnUiThread(new Runnable() {
@Override
public void run() {
final TextView textView=findViewById(R.id.textView);
textView.setText("Time:"+second);
}
});
}
});
timerViewModel.startTiming();
}
}
package com.example.myapplication;
import androidx.lifecycle.ViewModel;
import java.util.Timer;
import java.util.TimerTask;
public class TimerViewModel extends ViewModel {
private Timer timer;
private int currentSecond;
public void startTiming(){
if(timer==null)
{
currentSecond=0;
timer=new Timer();
TimerTask timerTask=new TimerTask() {
@Override
public void run() {
currentSecond++;
if(onTimeChangeListener!=null)
onTimeChangeListener.onTimeChanged(currentSecond);
}
};
timer.schedule(timerTask,500,1000);
}
}
public interface OnTimeChangeListener{
void onTimeChanged(int second);
}
public OnTimeChangeListener onTimeChangeListener;
public void setOnTimeChangeListener(OnTimeChangeListener onTimeChangeListener) {
this.onTimeChangeListener = onTimeChangeListener;
}
@Override
protected void onCleared() {
super.onCleared();
timer.cancel();
}
}
成果展示:
代码改进:通过Lifedata+ViewModel实现
package com.example.myapplication3_1;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import java.util.Timer;
import java.util.TimerTask;
public class TimerViewModel extends ViewModel {
private MutableLiveData<Integer> currentSecond;
private Timer timer;
public LiveData<Integer> getCurrentSecond() {
if (currentSecond == null) {
currentSecond = new MutableLiveData<>();
}
return currentSecond;
}
public void startTiming() {
if (timer == null) {
currentSecond.postValue(0);
timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
currentSecond.postValue(currentSecond.getValue()+1);
}
};
timer.schedule(timerTask, 500, 1000);
}
}
}
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView2;//声明一个文本框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iniComponent();
}
public void iniComponent() {
TimerViewModel timerViewModel = new ViewModelProvider(this).get(TimerViewModel.class);
timerViewModel.getCurrentSecond().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
Button button = findViewById(R.id.button1);
button.setText("" + integer);
}
});
timerViewModel.startTiming();
}
}