如何为倒数计时器添加时间? [英] How to add time to countdown timer?

查看:31
本文介绍了如何为倒数计时器添加时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有这样的帖子,但它没有明确回答问题.我有一个小游戏,你点击一个头,它移动到一个随机位置,你得到 +1 得分.同时有一个计时器从 60000(60 秒)开始倒计时并显示在下方.我怎样才能做到每当头部被点击时,它就会为计时器增加一秒?

I know there is a post like this but it does not answer the question clearly. I have a little game where you tap a head and it moves to a random position and you get +1 to score. Meanwhile there is a timer counting down from 60000 (60 seconds) and displaying below. How can I make it so whenever the head is tapped, it adds a second to the timer?

new CountDownTimer(timer, 1) {
    public void onTick(long millisUntilFinished) {
        textTimer.setText("Timer " + millisUntilFinished/1000);
    }
    public void onFinish() {
        Intent intent = new Intent(MainActivity.this, Gameover.class);
        startActivity(intent);
    }
}.start();

在 onClickListner 事件中,我有:

and in the onClickListner event I have:

timer=timer+1000;

它目前不起作用,因为点击时没有添加时间.

It currently doesn't work as in there is no time added on the click.

任何帮助将不胜感激:)

Any help would be appreciated :)

推荐答案

您无法更改预定计时器的时间.实现您正在尝试做的事情的唯一方法是取消计时器并设置一个新计时器.

You can't change the time of a scheduled timer. The only way to achieve what you are trying to do is by cancelling the timer and setting up a new one.

public class CountdownActivity extends Activity implements OnTouchListener{
    CountDownTimer mCountDownTimer;
    long countdownPeriod;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_countdown);
       countdownPeriod = 30000;
       createCountDownTimer();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mCountDownTimer != null)
                mCountDownTimer.cancel();
        createCountDownTimer();

        return true;
    }

    private void createCountDownTimer() {
        mCountDownTimer = new CountDownTimer(countdownPeriod + 1000, 1) {

            @Override
            public void onTick(long millisUntilFinished) {
                    textTimer.setText("Timer " + millisUntilFinished / 1000);
                countdownPeriod=millisUntilFinished;
            }

            @Override
            public void onFinish() {
                Intent intent = new Intent(MainActivity.this, Gameover.class);
                startActivity(intent);
            }
        };
    }
}

这篇关于如何为倒数计时器添加时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆