gpt4 book ai didi

Java Android : how to increment a chronometer by 2 seconds after you clicked a button?

转载 作者:行者123 更新时间:2023-12-02 00:59:05 25 4
gpt4 key购买 nike

我正在开发一个需要计时器的应用程序,但我找不到每次单击按钮时将计时器增加 2 秒的解决方案

//chronometer session
//private Chronometer chronometer;
//private boolean isRunning;
//private long pauseOffset;

//...

public void StartChronometer(){
if(!isRunning){
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
isRunning = true;
}

}

public void PauseChronometer(){
if(isRunning){
chronometer.stop();
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
isRunning = false;
}

}

public void ResetChronometer(){
chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;

}

public void IncrementByTwo(){
if(!isRunning){
chronometer.setBase(SystemClock.elapsedRealtime());
//????????????????????? NO IDEA ????????????????????????

}
}

我不知道如何增加它,我需要一个新的计时器在后台添加,当我单击按钮调用函数 IncrementByTwo 时,也许这就是全部? 2 个计时器,一个实时,一个在后台,每次我单击按钮时都会以某种方式添加第二个(荒谬或我不知道)

最佳答案

您可以使用一个变量来计算自SystemClock.elapsedRealtime()以来按钮被单击的次数。返回自应用程序启动以来的纳秒数,您可以添加 2000000000 * <your_variable> (2000000000 纳秒 => 2 秒)添加秒数:

private int i = 0;

// ...
public void PauseChronometer(){
if(isRunning){
chronometer.stop();
pauseOffset = (SystemClock.elapsedRealtime() + (i * 2000000000)) - chronometer.getBase();
isRunning = false;
}
}

public void ResetChronometer(){
i = 0;
chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
}

public void IncrementByTwo(){
if(!isRunning){
i++;
chronometer.setBase(SystemClock.elapsedRealtime() + (i * 2000000000));
}
}

关于Java Android : how to increment a chronometer by 2 seconds after you clicked a button?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57784378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com