CountDownTimer- that the user increments. Issues

女生的网名这么多〃 提交于 2019-12-19 04:12:48

问题


I have a question about CountDownTimer. I have to make an application that allows the user to increment the time time by +1, for every click of the button. Then after the button stops being clicked it waits for three seconds, then starts to countdown.

I pasted my code below.

My Issue is: I can't seem to get the Incrementing of the number working correctly, however it seems that after I stop Incrementing the number (onStop()) it directly goes to (onFinish()). Instead of going to the OnTick() and decreasing the number by 1 every second. I have tried numerous ways to fix this, but have been stuck.

Can anyone lead me in the right direction of what to do? Any help would be appreciated. Thank you guys!

 @SuppressWarnings("unused")
    public class MainActivity extends Activity {
        public int countdown;
        Button stoptime;
        public TextView timedisplay;
        public Timer wavetimer;
        private long millisInFuture;
        private long countDownInterval;
        private long onclicktime;
        private long finished;

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

        countdown = 01;
        stoptime = (Button) findViewById(R.id.button2);
        stoptime.setText("Stop Timer");

        timedisplay = (TextView) findViewById(R.id.mycounter);
        timedisplay.setText("0");
        wavetimer = new Timer (millisInFuture, 1000);
        finished = 0;
        stoptime.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) {
            wavetimer.onStop();
            //try{
            //  Thread.sleep(3000);
            //  wavetimer.start();

            //} catch (InterruptedException e) {

            //  e.printStackTrace();
        //  }


            //wavetimer.onTick(millisInFuture);
        }   

       });


    }



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



    public class Timer extends CountDownTimer  {
    public long millisInFuture;
    private long countDownInterval = 1000;
    private long currentelapsed;
    private long methodlimit;
    private long lapsedperiod;

    public Timer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }


    public void onFinish() {
        if (millisInFuture == 0){
        timedisplay.setText("Countdown Finished");
        } else {
            timedisplay.setText("error");

        }
    }

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

    }



    public void onTick(long millisUntilFinished) {
            millisInFuture = (millisInFuture - 1);
            timedisplay.setText("Time Left: " + millisInFuture / 1000);

        }

    } 

}

回答1:


add this into your code;

wavetimer.start();

:)



来源:https://stackoverflow.com/questions/12808201/countdowntimer-that-the-user-increments-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!