When I try to add two chronometers in the same activity, they always show the same time.

I am trying to implement a board game like chess and keep the time of players. So initially one chronometer runs and the other one waits, when one chronometer stops the other one starts.

But for example, when chrono1 shows 00:15 and chrono2 shows 00:00 if I stop chrono1 and start chrono2, it jumps to 00:15 and resumes from there while it is supposed to start from 00:00.

I use the following code:

Chronometer player1Chrono = (Chronometer)findViewById(R.id.player1Chrono);
Chronometer player2Chrono = (Chronometer)findViewById(R.id.player2Chrono);
player1Chrono.start();
...
//when player 1 makes a move
player2Chrono.start();
player1Chrono.stop();

Any idea how to solve this?

有帮助吗?

解决方案

Use this before starting player2Chrono,

player2Chrono.setBase(SystemClock.elapsedRealtime());
player2Chrono.start();

It will start from 0.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top