Start timer when button is pressed

I am new to Android programming, but I know Java. My question is, how does the timer work in Android? I read that it is better to use a handler. I want to do this, you press a button and the timer starts. By the time the button is pressed, everything is clear to me, but how do I start the timer?

+3


source to share


1 answer


How does the timer work in Android?

It is better to read the documentation on the timer , CountDownTimer Documentation and Documentation handler .

By the time the button is pressed, the button is cleared for me; but how can i start the timer?

If I did not understand your question, when you speak Timer

, you will refer to CounteDownTimer

. So, you should have something like this:

(I wrote some sample code. So, you have to figure it out first and then apply it to your code.)

Adding Buttons

btn1 = (Button)findViewById(R.id.bt1);
btn2 = (Button)findViewById(R.id.bt2);

      

Adding SetOnClickListener()

btn1.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {

    });
}

btn2.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {

    });
}

      

My btn1

starts CountDownTimer

and the second stops and clears it.

Now I am creating an Inner class named CountDownTimerTest

.

public class CountDownTimerTest extends CountDownTimer {
    public CountDownTimerTest(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onFinish() {
        text.setText("Time up!");
        timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
    }

    @Override
    public void onTick(long millisUntilFinished) {
        text.setText("Time remain:" + millisUntilFinished);
        timeElapsed = startTime - millisUntilFinished;
        timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
    }
}

      

Then on mine btn1

I put this code (run CountDownTimer

):

countDownTimer.start();

      

And on mine btn2

, I put this code (stop / cancel CountDownTimer

):



countDownTimer.cancel();

      

Now I hope you can understand how it works CountDownTimer

, if your question is not about CountDownTimer

let me know and I will update my answer as soon as possible with your wishes.

EDIT - with just one Button

To do it with the same Button

, you can do this:

Create a variable Boolean

like:

Boolean ButtonClicked = false;

      

Then change the code as follows:

btn1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        if (!ButtonClicked)) {
            ButtonClicked = true;
            countDownTimer.start(); 
        } else {
            ButtonClicked = false;
            countDownTimer.cancel();
        }                       
    });
}

      

EDIT 2 Get button click

You can create int

one called NumberButtonClicked

like this:

int NumberButtonClicked = 0;

      

Then on each Button

you have to do this (example):

btn1.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {
            NumberButtonClicked = 1;
    });
}

      

Then you know that if you clicked btn1

, your variable will be 1.

+6


source







All Articles