将文本从一个类中移出。

huangapple go评论93阅读模式
英文:

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

  1. public class Countdown_test {
  2. private static long START_TIME_IN_MILLIS;
  3. private CountDownTimer mCountDownTimer;
  4. private boolean mTimerRunning;
  5. private long mTimeLeftInMillis;
  6. //private TextView timer;
  7. public Countdown_test(long start_time) {
  8. START_TIME_IN_MILLIS = start_time;
  9. mTimeLeftInMillis = START_TIME_IN_MILLIS;
  10. //timer = findViewById(R.id.timer); doesn't work
  11. }
  12. public void startTimer() {
  13. mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
  14. @Override
  15. public void onTick(long millisUntilFinished) {
  16. mTimeLeftInMillis = millisUntilFinished;
  17. updateCountDownText();
  18. }
  19. @Override
  20. public void onFinish() {
  21. mTimerRunning = false;
  22. }
  23. }.start();
  24. mTimerRunning = true;
  25. }
  26. public void updateCountDownText() {
  27. int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
  28. int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
  29. String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
  30. System.out.println(timeLeftFormatted);
  31. }
  32. }

MainActivity.java

  1. Countdown_test ct = new Countdown_test(600000);
  2. button_start.setOnClickListener(v -> {
  3. ct.startTimer();
  4. });
  5. 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

  1. public class Countdown_test {
  2. private static long START_TIME_IN_MILLIS;
  3. private CountDownTimer mCountDownTimer;
  4. private boolean mTimerRunning;
  5. private long mTimeLeftInMillis;
  6. //private TextView timer;
  7. public Countdown_test(long start_time) {
  8. START_TIME_IN_MILLIS = start_time;
  9. mTimeLeftInMillis = START_TIME_IN_MILLIS;
  10. //timer = findViewById(R.id.timer); dosen't work
  11. }
  12. public void startTimer() {
  13. mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
  14. @Override
  15. public void onTick(long millisUntilFinished) {
  16. mTimeLeftInMillis = millisUntilFinished;
  17. updateCountDownText();
  18. }
  19. @Override
  20. public void onFinish() {
  21. mTimerRunning = false;
  22. }
  23. }.start();
  24. mTimerRunning = true;
  25. }
  26. public void updateCountDownText() {
  27. int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
  28. int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
  29. String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
  30. System.out.println(timeLeftFormatted);
  31. }
  32. }

MainActivity.java

  1. Countdown_test ct = new Countdown_test(600000);
  2. button_start.setOnClickListener(v -> {
  3. ct.startTimer();
  4. });
  5. ct.updateCountDownText();

答案1

得分: 0

你可以编写一个接口,一个带有方法onTimeLeftChanged(String timeleft)TimeLeftListener。然后,你的活动继承TimeLeftListener并实现onTimeLeftChanged()方法。
这是你更新视图的地方。
当你初始化CountDownTest时,可以像这样传递活动:

  1. Countdown_test ct = new Countdown_test(600000, this);

并在你的CountDownTest类中创建一个监听器,像这样:

  1. myTimeLeftListener = (TimeLeftListener) activity;

然后在你的updateCountDownText()方法中,你可以添加:

  1. 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.

  1. Countdown_test ct = new Countdown_test(600000, this);

And create al listener in you CountDownTest-class like this

  1. myTimeLeftListener =(TimeleftListener)activity;

Then in your updateCountDownText()-Method you put

  1. myTimeLeftListener.onTimeLeftChanged(timeleft)

You could also use ViewModel and LiveData.

huangapple
  • 本文由 发表于 2020年8月4日 18:52:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63245335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定