循环中的计数和倒计时。安卓

时间:2015-06-28 01:51:22

标签: android eclipse counter countdown

我想数从1到15,例如从15到1到1并重复这个过程......请你帮我完成一下?我尝试了一切。而不是如果,我尝试了,但由于某种原因,它给出了随机数。此方法仅减去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;

2 个答案:

答案 0 :(得分:1)

你没有提供整个代码,但仍然

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

继承我的解决方案:

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

检查此布尔值如果countingDown为true / false,该怎么做。

像:

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
}

干杯

答案 1 :(得分:0)

最后。我做的。 Noob错误: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);