英文:
Change text from a class out
问题
I created a simple Java class. This should spend the time. With System.out.println it works too! But how can I inform a certain TextView that it should also change?
Countdown_Test.java
public class Countdown_test {
private static long START_TIME_IN_MILLIS;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis;
//private TextView timer;
public Countdown_test(long start_time) {
START_TIME_IN_MILLIS = start_time;
mTimeLeftInMillis = START_TIME_IN_MILLIS;
//timer = findViewById(R.id.timer); doesn't work
}
public void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
@Override
public void onFinish() {
mTimerRunning = false;
}
}.start();
mTimerRunning = true;
}
public void updateCountDownText() {
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
System.out.println(timeLeftFormatted);
}
}
MainActivity.java
Countdown_test ct = new Countdown_test(600000);
button_start.setOnClickListener(v -> {
ct.startTimer();
});
ct.updateCountDownText();
英文:
I created a simple Java class. This should spend the time. With System.out.println it works too! But how can I inform a certain TextView that it should also change?
Countdown_Test.java
public class Countdown_test {
private static long START_TIME_IN_MILLIS;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis;
//private TextView timer;
public Countdown_test(long start_time) {
START_TIME_IN_MILLIS = start_time;
mTimeLeftInMillis = START_TIME_IN_MILLIS;
//timer = findViewById(R.id.timer); dosen't work
}
public void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
@Override
public void onFinish() {
mTimerRunning = false;
}
}.start();
mTimerRunning = true;
}
public void updateCountDownText() {
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
System.out.println(timeLeftFormatted);
}
}
MainActivity.java
Countdown_test ct = new Countdown_test(600000);
button_start.setOnClickListener(v -> {
ct.startTimer();
});
ct.updateCountDownText();
答案1
得分: 0
你可以编写一个接口,一个带有方法onTimeLeftChanged(String timeleft)
的TimeLeftListener
。然后,你的活动继承TimeLeftListener
并实现onTimeLeftChanged()
方法。
这是你更新视图的地方。
当你初始化CountDownTest
时,可以像这样传递活动:
Countdown_test ct = new Countdown_test(600000, this);
并在你的CountDownTest
类中创建一个监听器,像这样:
myTimeLeftListener = (TimeLeftListener) activity;
然后在你的updateCountDownText()
方法中,你可以添加:
myTimeLeftListener.onTimeLeftChanged(timeleft);
你也可以使用ViewModel和LiveData。
英文:
You could write an interface, a TimeLeftListener with a method onTimeLeftChanged(String timeleft). Then your activity extends TimeLeftListener and implements the onTimeLeftChanged()-method.
This is where you update your views.
When you initiate your CountDownTest you pass the activity like this.
Countdown_test ct = new Countdown_test(600000, this);
And create al listener in you CountDownTest-class like this
myTimeLeftListener =(TimeleftListener)activity;
Then in your updateCountDownText()-Method you put
myTimeLeftListener.onTimeLeftChanged(timeleft)
You could also use ViewModel and LiveData.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论