小编典典

递增countDownTimer Android

java

对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);
}

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


阅读 250

收藏
2020-11-30

共1个答案

小编典典

您应该知道,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;
    }
}
2020-11-30