我正在尝试在一个活动中使用多个计时器,但是它们中每个计时器的行为都不如预期。
我期望能够同时启动每个计时器,并且为每个变量声明了不同的功能。
代码
timeWhenStoppedSetup = 0;
timeWhenStoppedProcessing = 0;
timeWhenStoppedReconciliation = 0;
timeWhenStoppedCleaning = 0;
chronometerSetUp.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometerChanged) {
chronometerSetUp = chronometerChanged;
long time = SystemClock.elapsedRealtime() - chronometerSetUp.getBase();
int h = (int)(time /3600000);
int m = (int)(time - h*3600000)/60000;
int s= (int)(time - h*3600000- m*60000)/1000 ;
String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
chronometerSetUp.setText(t);
}
});
startSetUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startStopChronometerSetUp(startSetUp);
}
});
chronometerProcessing.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometerChanged) {
chronometerProcessing = chronometerChanged;
long time = SystemClock.elapsedRealtime() - chronometerProcessing.getBase();
int h = (int)(time /3600000);
int m = (int)(time - h*3600000)/60000;
int s= (int)(time - h*3600000- m*60000)/1000 ;
String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
chronometerProcessing.setText(t);
}
});
startProcessing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startStopChronometerProcessing(startProcessing);
}
});
chronometerReconciliation.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometerChanged) {
chronometerReconciliation = chronometerChanged;
long time = SystemClock.elapsedRealtime() - chronometerReconciliation.getBase();
int h = (int)(time /3600000);
int m = (int)(time - h*3600000)/60000;
int s= (int)(time - h*3600000- m*60000)/1000 ;
String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
chronometerReconciliation.setText(t);
}
});
startReconciliation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startStopChronometerReconciliation(startReconciliation);
}
});
chronometerCleaning.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometerChanged) {
chronometerCleaning = chronometerChanged;
long time = SystemClock.elapsedRealtime() - chronometerCleaning.getBase();
int h = (int)(time /3600000);
int m = (int)(time - h*3600000)/60000;
int s= (int)(time - h*3600000- m*60000)/1000 ;
String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
chronometerCleaning.setText(t);
}
});
startCleaning.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startStopChronometerCleaning(startCleaning);
}
});
}
public void startStopChronometerSetUp(View view){
if(isStart){
chronometerSetUp.stop();
timeWhenStoppedSetup = chronometerSetUp.getBase() - SystemClock.elapsedRealtime();
isStart = false;
}else{
chronometerSetUp.setBase(SystemClock.elapsedRealtime() + timeWhenStoppedSetup);
chronometerSetUp.start();
isStart = true;
((Button)view).setBackgroundResource(stop);
}
}
public void startStopChronometerProcessing(View view){
if(isStart){
chronometerProcessing.stop();
timeWhenStoppedProcessing = chronometerProcessing.getBase() - SystemClock.elapsedRealtime();
isStart = false;
}else{
chronometerProcessing.setBase(SystemClock.elapsedRealtime() + timeWhenStoppedProcessing);
chronometerProcessing.start();
isStart = true;
((Button)view).setBackgroundResource(stop);
}
}
public void startStopChronometerReconciliation(View view){
if(isStart){
chronometerReconciliation.stop();
timeWhenStoppedReconciliation = chronometerReconciliation.getBase() - SystemClock.elapsedRealtime();
isStart = false;
}else{
chronometerReconciliation.setBase(SystemClock.elapsedRealtime() + timeWhenStoppedReconciliation);
chronometerReconciliation.start();
isStart = true;
((Button)view).setBackgroundResource(stop);
}
}
public void startStopChronometerCleaning(View view){
if(isStart){
chronometerCleaning.stop();
timeWhenStoppedCleaning = chronometerCleaning.getBase() - SystemClock.elapsedRealtime();
isStart = false;
}else{
chronometerCleaning.setBase(SystemClock.elapsedRealtime() + timeWhenStoppedCleaning);
chronometerCleaning.start();
isStart = true;
((Button)view).setBackgroundResource(stop);
}
}
我在这里做错什么了吗?
其他天文台表的行为只是随机的。可以在随机时间等开始。