restart a CountDownTimer

空扰寡人 提交于 2021-02-08 09:54:41

问题


With android studio I've an app with a CountDownTimer. It starts from 10 seconds to 0 when I press a button and a TextView that count how many clicks i do on a button,so far so good. I want to restart this activity all times i want by pressing another button restart. Can you help me? If it helps I put the code.

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

    txtCount = (TextView)findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount = (Button)findViewById(R.id.button1);
    btnRestart = (Button)findViewById(button2);

    final boolean[] timerProcessing = {false};
    final boolean[] timerStarts = {false};

    final TextView textViewTimer = (TextView)findViewById(R.id.textView2);
    //Saving link to timer object
    final CountDownTimer timer = new CountDownTimer(10000, 1) {

        public void onTick(long millisUntilFinished) {
            textViewTimer.setText("" + millisUntilFinished / 1000
                    + ":" + millisUntilFinished % 1000);
                        }


        public void onFinish() {
            textViewTimer.setText("0:000");
            timerProcessing[0] = false;
        }

    };

    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            //start timer once when button first click
            if (!timerStarts[0]){
                timer.start();
                timerStarts[0] = true;
                timerProcessing[0] = true;
            }

            if (timerProcessing[0]){
                count++;
                txtCount.setText(String.valueOf(count));
            }
        }
    });
    btnRestart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {  }
}

回答1:


Just replace below code:

private TextView txtCount, textViewTimer;
private Button btnCount, btnRestart;
int count = 0;
boolean[] timerProcessing = { false };
boolean[] timerStarts = { false };
private MyCount timer;

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

    txtCount = (TextView) findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount = (Button) findViewById(R.id.button1);
    btnRestart = (Button) findViewById(R.id.button2);

    textViewTimer = (TextView) findViewById(R.id.textView2);

    timer = new MyCount(10000, 1);

    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            // start timer once when button first click
            if (!timerStarts[0]) {
                timer.start();
                timerStarts[0] = true;
                timerProcessing[0] = true;
            }

            if (timerProcessing[0]) {
                count++;
                txtCount.setText(String.valueOf(count));
            }
        }
    });
    btnRestart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            timerProcessing[0] = true;
            count = 0;
            txtCount.setText(String.valueOf(count));
            timer.cancel();
            timer.start();
        }
    });
}

public class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        textViewTimer.setText("0:000");
        timerProcessing[0] = false;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        textViewTimer.setText("" + millisUntilFinished / 1000 + ":"
                + millisUntilFinished % 1000);

    }
}

Here your counter varibale is replaced with a inner class so that you dont need to create counter variable each and every time. Just create once counter variable and call start method of it if you want to restart the counter.




回答2:


Wrap the CountDownTimer in a method then call it again when you click the restart button.




回答3:


Why do you have to restart the Activity. You need to have a logic and handle the restart buttons onclicklistener.

Move your countdowntimer code to a function and invoke this function from your activities onCreate/onResume and also from restart buttons click.



来源:https://stackoverflow.com/questions/19415360/restart-a-countdowntimer

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