如何在运行期间递增倒计时器?[英] How to increment countdown timer during runtime?

本文是小编为大家收集整理的关于如何在运行期间递增倒计时器?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在制作一个课程项目和这个项目,我需要显示倒计时计时器.将有一个按钮定时器递增5 +秒.例如,如果定时器设置并在1:00启动,并且如果用户单击按钮,则计时器必须增加+5,如1:05.

我的代码:

public class MainActivity extends Activity {

Button incrementTime, startTime;
public TextView timedisplay;
long millisInFuture = 1000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    incrementTime = (Button) findViewById(R.id.button2);
    startTime = (Button) findViewById(R.id.button3);
    timedisplay = (TextView) findViewById(R.id.mycounter);

    resetText();

    incrementTime.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            millisInFuture += 1000;
            resetText();
        }
    });

    startTime.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            CountDownTimer wavetimer = new myTimer(millisInFuture + 3000, 1000).start();
            // ^ add 3 seconds.
        }
    });}

protected void resetText() {
    timedisplay.setText("Time Left: " + millisInFuture / 1000);
}

public class myTimer extends CountDownTimer  {

    private long millisActual;

    public myTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        millisActual = millisInFuture - 3000;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        //v start showing the tick after 3 seconds.
        if (millisUntilFinished <= millisActual) {
            timedisplay.setText("Time Left: " + millisUntilFinished / 1000);                
        }
    }

    @Override
    public void onFinish() {
        timedisplay.setText("Countdown Finished");
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }
}

我的问题:

这与我想要的方式相同,但在计时器完成运行后它只递增.在倒计时运行时,我希望它递增!

推荐答案

您可以使用此类满足您的需求:

    public abstract class MyCountDownTimer {

    private CountDownTimer cdt;
    private long millisInFuture;
    private long countDownInterval;

    public MyCountDownTimer(long millisInFuture, long countDownInterval) {
        this.millisInFuture = millisInFuture;
        this.countDownInterval = countDownInterval;

        recreateCounter(millisInFuture, countDownInterval);
    }

    public abstract void onFinish();

    public abstract void onTick(long millisUntilFinished);

    public void start(){
        cdt.start();
    }

    private void setMillisInFuture(long millisInFuture){
        this.millisInFuture = millisInFuture;
    }

    public void onIncrement(long millis){
        millisInFuture += millis;
        recreateCounter(millisInFuture, countDownInterval);
    }

    private void recreateCounter(long millisInFuture, long countDownInterval){
        if(cdt != null){
            try {
                cdt.cancel();
            } catch (Exception e) {
            }
        }

        cdt = new CountDownTimer(millisInFuture, countDownInterval) {

            @Override
            public void onTick(long millisUntilFinished) {
                setMillisInFuture(millisUntilFinished);
                MyCountDownTimer.this.onTick(millisUntilFinished);
            }

            @Override
            public void onFinish() {
                MyCountDownTimer.this.onFinish();
            }
        };
    }
}

本文地址:https://www.itbaoku.cn/post/102760.html

问题描述

I am making a course project and for this project, I will need to show a countdown timer. There will be a button which increments the timer by 5+ second. For example if the timer is set and started at 1:00 and if a user clicks the button, the timer must increase +5 like 1:05.

My code:

public class MainActivity extends Activity {

Button incrementTime, startTime;
public TextView timedisplay;
long millisInFuture = 1000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    incrementTime = (Button) findViewById(R.id.button2);
    startTime = (Button) findViewById(R.id.button3);
    timedisplay = (TextView) findViewById(R.id.mycounter);

    resetText();

    incrementTime.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            millisInFuture += 1000;
            resetText();
        }
    });

    startTime.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            CountDownTimer wavetimer = new myTimer(millisInFuture + 3000, 1000).start();
            // ^ add 3 seconds.
        }
    });}

protected void resetText() {
    timedisplay.setText("Time Left: " + millisInFuture / 1000);
}

public class myTimer extends CountDownTimer  {

    private long millisActual;

    public myTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        millisActual = millisInFuture - 3000;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        //v start showing the tick after 3 seconds.
        if (millisUntilFinished <= millisActual) {
            timedisplay.setText("Time Left: " + millisUntilFinished / 1000);                
        }
    }

    @Override
    public void onFinish() {
        timedisplay.setText("Countdown Finished");
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }
}

My problem:

This works the same way I want but it only increments after the timer has finished running. I want it to increment during the countdown runtime!

推荐答案

You can use this class for your needs:

    public abstract class MyCountDownTimer {

    private CountDownTimer cdt;
    private long millisInFuture;
    private long countDownInterval;

    public MyCountDownTimer(long millisInFuture, long countDownInterval) {
        this.millisInFuture = millisInFuture;
        this.countDownInterval = countDownInterval;

        recreateCounter(millisInFuture, countDownInterval);
    }

    public abstract void onFinish();

    public abstract void onTick(long millisUntilFinished);

    public void start(){
        cdt.start();
    }

    private void setMillisInFuture(long millisInFuture){
        this.millisInFuture = millisInFuture;
    }

    public void onIncrement(long millis){
        millisInFuture += millis;
        recreateCounter(millisInFuture, countDownInterval);
    }

    private void recreateCounter(long millisInFuture, long countDownInterval){
        if(cdt != null){
            try {
                cdt.cancel();
            } catch (Exception e) {
            }
        }

        cdt = new CountDownTimer(millisInFuture, countDownInterval) {

            @Override
            public void onTick(long millisUntilFinished) {
                setMillisInFuture(millisUntilFinished);
                MyCountDownTimer.this.onTick(millisUntilFinished);
            }

            @Override
            public void onFinish() {
                MyCountDownTimer.this.onFinish();
            }
        };
    }
}