0

对 CountDownTimer 有疑问。我必须制作一个应用程序,允许用户在每次单击按钮时将时间时间增加 +1。然后在按钮停止单击后等待三秒钟,然后开始倒计时。

我在下面粘贴了我的代码。

我的问题是:我似乎无法使数字的递增正常工作,但是似乎在我停止递增数字(onStop())后,它直接转到(onFinish())。而不是去 OnTick() 并每秒将数字减少 1。我尝试了很多方法来解决这个问题,但都被卡住了。

谁能带领我朝着正确的方向前进?任何帮助,将不胜感激。谢谢你们!

public class MainActivity extends Activity {

    Button incrementTime, startTime;
    public TextView timedisplay;
    public myTimer wavetimer;
    private long millisInFuture;
    private long millisUntilFinished;
    private long countDownInterval;
    private long onclicktime;
    //private WaveInterface model;
    public int countdown;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    incrementTime = (Button) findViewById(R.id.button2);
    startTime = (Button) findViewById(R.id.button3);
    incrementTime.setText("Increment Time");
    startTime.setText("Start");
    timedisplay = (TextView) findViewById(R.id.mycounter);
    timedisplay.setText("Time Left: " + millisUntilFinished);
    wavetimer = new myTimer (millisInFuture, 1000);


    incrementTime.setOnClickListener(new OnClickListener(){
    int countdown = 01;
   /**
    * On click button Listner
 * @return 
    */
    public void onClick(View v) {
        millisInFuture++;
        return; 
    }

   });
    startTime.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
        wavetimer.start();
    }
});}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


public class myTimer extends CountDownTimer  {
public long millisInFuture;
private long countDownInterval;


public myTimer(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
    millisUntilFinished--;
    if (millisUntilFinished == 0){
        wavetimer.onFinish();
    }
    else {
    timedisplay.setText("Time Left: " + millisUntilFinished / 1000);
    }

}
@Override
public void onFinish() {
    timedisplay.setText("Countdown Finished");
    wavetimer.cancel();
}


public void onStop() {
    millisInFuture = millisInFuture + 1;
    timedisplay.setText("Time Left: " + millisInFuture);
}

我似乎无法弄清楚如何正确增加数字。我尝试了多种方法,它一直在搞砸。不知道在哪里看。任何帮助,将不胜感激。谢谢你们!

4

2 回答 2

2

您应该知道,当倒计时课程的时间到了时,您不必手动调用onFinish,也不必取消,onFinish因为一个导致另一个或手动递减任何内容。尝试以下操作,看看它是否符合您的想法。希望您能注意到,我没有看到实际的理由来查看按钮是否“不再被点击”。

public class MainActivity extends Activity {

    Button incrementTime, startTime;
    public TextView timedisplay;
    long millisInFuture = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        incrementTime = (Button) findViewById(R.id.button2);
        startTime = (Button) findViewById(R.id.button3);
        timedisplay = (TextView) findViewById(R.id.mycounter);

        resetText();

        incrementTime.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                millisInFuture += 1000;
                resetText();
            }
        });

        startTime.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                CountDownTimer wavetimer = new myTimer(millisInFuture + 3000, 1000).start();
                // ^ add 3 seconds.
            }
        });}

    protected void resetText() {
        timedisplay.setText("Time Left: " + millisInFuture / 1000);
    }

    public class myTimer extends CountDownTimer  {

        private long millisActual;

        public myTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            millisActual = millisInFuture - 3000;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            //v start showing the tick after 3 seconds.
            if (millisUntilFinished <= millisActual) {
                timedisplay.setText("Time Left: " + millisUntilFinished / 1000);                
            }
        }

        @Override
        public void onFinish() {
            timedisplay.setText("Countdown Finished");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
于 2012-11-21T03:20:05.243 回答
1

在您的活动 xml 文件中添加名称为“timerText”的 TextView

在类中添加以下两个全局变量

int repeatCounter=1;//incrementing for every 60 sec
CountDownTimer tripTimeCounter;

将以下方法添加到类并从活动 onCreate 或您想要的任何其他地方调用它

public void startTimeCounter(){
    tripTimeCounter=new CountDownTimer(60*1000, 1000){

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            repeatCounter=repeatCounter+1;
            startTimeCounter();//follow the recursion on finish of the limit of 60 seconds & increment the repeat counter
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub

            timerText.setText(formatMillis((repeatCounter*60)*1000-millisUntilFinished));
        }

    }.start();
}

要以时间格式显示值,请使用以下方法

 static public String formatMillis(long val) {
    StringBuilder                       buf=new StringBuilder(20);
    String                              sgn="";

    if(val<0) { sgn="-"; val=Math.abs(val); }

    append(buf,sgn,0,( val/3600000));
    append(buf,":",2,((val%3600000)/60000));
    append(buf,":",2,((val %60000)/1000));
    //append(buf,".",3,( val%1000));
    return buf.toString();
 }
于 2013-04-08T11:43:03.863 回答