首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓:停止之前的CountDownTimer

安卓:停止之前的CountDownTimer
EN

Stack Overflow用户
提问于 2012-12-22 19:38:49
回答 1查看 1.8K关注 0票数 0

我现在正在做一个游戏,首先玩家将看到按钮的颜色,然后5秒后所有按钮的颜色将消失,用户必须选择那些具有相同颜色的按钮才能获胜。

对于这5秒的等待,我使用了handler来等待执行,同时显示CountDownTimer对这5秒的倒计时,让玩家知道剩下的时间有多长。

如果用户放弃当前游戏,则可以单击重新启动按钮开始另一个游戏。

一切运行正常,当玩家点击重新启动按钮时,程序也知道需要等待5秒才能清除所有按钮的颜色。

问题:

这是关于5秒倒计时的显示。如果用户开始1场游戏,倒计时开始计时。如果用户再次按下重新启动按钮,例如,3秒后,原来的倒计时不会终止,而是同时显示两个倒计时。我想问一下,如果玩家按下重新开始按钮,如何终止之前的倒计时?

代码:

代码语言:javascript
运行
复制
protected void onCreate(Bundle savedInstanceState) 
{
        ...other actions

    restart.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            handler1.removeCallbacks(txtClearRun);
            SetNewQ();
        }
    });

    SetNewQ();      

} // end onCreate

private void SetNewQ() 
{       
    MyCount5sec counter5sec = new MyCount5sec(5000,1000);
    counter5sec.start();
    ...other actions below
}   

   public class MyCount5sec extends CountDownTimer
   {
       public MyCount5sec(long millisInFuture, long countDownInterval) {super(millisInFuture, countDownInterval);}
       @Override
       public void onFinish() 
       {
           ButtonRemainTime= (Button) findViewById(R.id.button_remaintime); 
           ButtonRemainTime.setText("Add oil!!");
           // game then start by blanking out all the colors
       }
       @Override
       public void onTick(long millisUntilFinished) 
       {
           ButtonRemainTime= (Button) findViewById(R.id.button_remaintime); 
           ButtonRemainTime.setText("" + millisUntilFinished/1000);
       }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-22 21:53:06

我知道你的问题是什么,但我个人会用下面的方式调整我的设置。如果您的CountDownTimer类没有实现太多其他的东西,那么您实际上可以将该类声明为field并将其一举实例化。

代码语言:javascript
运行
复制
protected void onCreate(Bundle savedInstanceState) 
{
        ...other actions

    restart.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            handler1.removeCallbacks(txtClearRun);
            MyCount5sec.start(); // ----------changed this line
        }
    });

    MyCount5sec.start(); // ---------- changed this line

} // end onCreate

CountDownTimer MyCount5sec = new CountDownTimer(5000,1000) {

    @Override
    public void onTick(long millisUntilFinished) {
           ButtonRemainTime= (Button) findViewById(R.id.button_remaintime); 
           ButtonRemainTime.setText("" + millisUntilFinished/1000);         
    }

    @Override
    public void onFinish() {
           ButtonRemainTime= (Button) findViewById(R.id.button_remaintime); 
           ButtonRemainTime.setText("Add oil!!");
           // game then start by blanking out all the colors            
    }
}; 

这样做的好处是您将始终引用相同的对象。您不必在每个onClick上创建新的计时器,然后再取消另一个计时器。实际上,您甚至根本不需要cancel,因为在这个timer对象的早期调用timer的start将会重新启动它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14002515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档