简体   繁体   English

在一个活动中的两个计时表视图

[英]Two Chronometer view in a single Activity

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. 但是例如,当chrono1显示00:15并且chrono2显示00:00如果我停止chrono1并启动chrono2时,它跳转到00:15并从那里恢复,而它应该从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之前使用它,

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

It will start from 0. 它将从0开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM