【发布时间】: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