将文本从一个类中移出。

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

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.

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:

确定