Domanda

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) {  }
}
È stato utile?

Soluzione

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.

Altri suggerimenti

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

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top