CountDownTimer- that the user increments. Issues

2019-02-15 06:18发布

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条回答
趁早两清
2楼-- · 2019-02-15 06:28

add this into your code;

wavetimer.start();

:)

查看更多
登录 后发表回答