【问题标题】:countup and countdown in a loop. android倒计时和倒计时循环。安卓
【发布时间】:2015-06-28 01:51:22
【问题描述】:

例如,我想从 1 数到 15,然后从 15 到 1 并重复该过程...请您帮我完成吗?我什么都试过了。而不是 if,我尝试了 While,它出于某种原因给出了随机数。这种方法只减到 14。它不会完全倒计时。

int counter = 0;
int total = 15;
number = (TextView) this.findViewById(R.id.number);


    final Timer c=new Timer();
    c.scheduleAtFixedRate(new TimerTask() {         
            @Override
            public void run() {

                if (counter < total && timerHasStarted ) {


                    runOnUiThread(new Runnable()

                    {
                        @Override
                        public void run()
                        {
                           number.setText("" +counter );
                            counter++;    

                        }
                    });

                }
              if (counter <= total && timerHasStarted ) {


                    runOnUiThread(new Runnable()

                    {
                        @Override
                        public void run()
                        {
                           number.setText("" +counter );
                            counter++;    

                        }
                    });
                    if (counter == total && timerHasStarted) {
                        countingDown = true;
                      if(countingDown){

                            counter--;
                        }
                        else{
                            counter++;

                        }
                        //setting the text here
                        if(counter%15==0){    //only 0 when counter equals 0 or 15
                            countingDown=!countingDown; // starting the other direction at next time
                        }



                    }

                }}}, 1000, 300);

    timerHasStarted = true;

【问题讨论】:

    标签: android eclipse counter countdown


    【解决方案1】:

    你没有提供完整的代码,但是

    您的逻辑有一个很大的缺陷:第一个计数器是 0,所以第一个 if 会计数它,因为它低于 15...它会一直计数到 15 然后它什么也不做...但是第二个来了如果现在该计数器是 15,它会执行使计数器再次回到 14,因此它会在 14 和 15 之间改变它第一次到达的点。

    这是我的解决方案:

    有一个类似countingDown的布尔值,如果达到15,则在开始时将其设置为false,将其设置为true。

    检查这个布尔值,如果countingDown 为真/假,该怎么做。

    喜欢:

    if(countingDown){
        counter--;
    }
    else{
        counter++;
    }
    //setting the text here
    if(counter%15==0){    //only 0 when counter equals 0 or 15
        countingDown=!countingDown; // starting the other direction at next time
    }
    

    干杯

    【讨论】:

    • 能否请您澄清更多?我是否保留第一个计数代码?好吧,奇怪的是总数= 15,但计数器从未达到15,它达到14。我将计数器更改为1。如果我不太了解你,请原谅,我是新手。我试过你的例子,但它似乎一开始就数不过来。我希望你知道我想要什么。我希望计数器达到总计,然后减少直到达到 1,这是它的原始 int,然后重新计数到 15 并保持重复。
    • 是的,把这段代码放在你的计时器方法中,它会在 0 和 15 中工作。
    • 好吧,我照你说的做了,它停留在总数为 15。它不会倒计时。谢谢你陪我。 :p 有些事情不对劲。请再提供帮助将不胜感激。
    【解决方案2】:

    最后。我做到了。菜鸟错误:p

    final Timer c=new Timer();
        c.scheduleAtFixedRate(new TimerTask() {         
                @Override
                public void run() {
    
                    if (counter <= total && timerHasStarted ) {
    
    
                        runOnUiThread(new Runnable()
    
                        {
                            @Override
                            public void run()
                            {
                               number.setText("" +counter );
                                  // removed counter++;
    
                            }
                        });
                        if (counter == total && timerHasStarted) {
    
                             runOnUiThread(new Runnable()
    
                            {
                                @Override
                                public void run()
                                {
                                   number.setText("" +counter);
                                           // removed counter--;
    
                                }
    
                            });
    
    
    
                            }
                          if(countingDown){
                              countingDown = true;
                                counter--;
                            }
                            else{
                                counter++;
                                countingDown = false;
                            }
                            //setting the text here
                            if(counter%15==0){    //only 0 when counter equals 0 or 15
                                countingDown=!countingDown; // starting the other direction at next time
                            }
    
    
    
                        }
    
                    }}, 1000, 300);
    

    【讨论】:

      猜你喜欢
      • 2014-01-08
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多