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

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

问题描述

我知道有这样一个职位,但它并没有回答这个问题清楚了。 我有一个小游戏,你点击一个头并将其移动到一个随机位置,你会得到+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.

任何帮助将是AP preciated:)

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天全站免登陆