gpt4 book ai didi

java - 暂停和恢复计时器而不重置计时器

转载 作者:行者123 更新时间:2023-12-01 12:07:44 24 4
gpt4 key购买 nike

是否有任何简单的方法,当计时器暂停然后再次启动时,它会从暂停的时间恢复?

我查看了其他教程,他们只有在按下暂停按钮两次后才会重新启动计时器。

我是 Android 开发新手,如果这是一个愚蠢的问题,我很抱歉。

public class Timer extends Fragment {

TextView CurrentDate;
TextView CurrentTime;
Thread ThreadTime;
TextView textTimeStarted;
Button buttonReset;
Button buttonPauseStart;
TextView textElapsedTime;
private Button startButton;
private Button pauseButton;
private TextView timerValue;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
long startTime = 0L;

//runs without a timer by reposting this handler at the end of the runnable
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
@Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
int millisecs = (int) (millis % 1000);
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;

textElapsedTime.setText(String.format("%d:%02d:%02d", minutes, seconds, millisecs));

timerHandler.postDelayed(this, 0);
}
};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View T = inflater.inflate(R.layout.time_layout, container, false);

Runnable myRunnableThread = new CountDownRunner();
ThreadTime = new Thread(myRunnableThread);
ThreadTime.start();
ConnecttoXML(T);
buttonReset = (Button) T.findViewById(R.id.buttonStop);
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timerHandler.removeCallbacks(timerRunnable);
textElapsedTime.setText("");
textTimeStarted.setText("");
}
});

buttonPauseStart = (Button) T.findViewById(R.id.ButtonStart);
buttonPauseStart.setText("Start");


buttonPauseStart.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
buttonPauseStart = (Button) v;
if (buttonPauseStart.getText().equals("stop")) {
timerHandler.removeCallbacks(timerRunnable);
buttonPauseStart.setText("start");

} else {

startTime = System.currentTimeMillis();
timerHandler.postDelayed(timerRunnable, 0);
Calendar cs = Calendar.getInstance();
System.out.println("Current time => " + cs.getTime());

SimpleDateFormat df = new SimpleDateFormat("HH:mm");
String formattedDate = df.format(cs.getTime());

textTimeStarted.setText(formattedDate);
buttonPauseStart.setText("stop");
}
}
});

return T;
}

private void ConnecttoXML(View view) {

CurrentDate = (TextView) view.findViewById(R.id.textCurrentDate);
textTimeStarted = (TextView) view.findViewById(R.id.textTimeStarted);
textElapsedTime = (TextView) view.findViewById(R.id.textElapsedTime);

String currentDate = DateFormat.getDateInstance().format(Calendar.getInstance().getTime());
CurrentDate.setText(currentDate);
}

public void doWork() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
try {
CurrentTime = (TextView) getActivity().findViewById(R.id.textCurrentTime);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
String curTime = hours + ":" + minutes;
CurrentTime.setText(curTime);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


public class CountDownRunner implements Runnable {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
doWork();
Thread.sleep(1000); // Pause of 1 Second
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
}
}
}
}
}

最佳答案

不,对于处理程序,您只能设置时间然后取消它,但我不知道这对您有何帮助,因为您在 0 处运行 postDelayed,本质上是尽可能快地更新计时器在你的用户界面中。

如果您想暂停计时器,则必须保存耗时而不是开始时间,并具有开始/停止和重置按钮。

这是我发现的向计时器添加暂停的最简单方法:

buttonPauseStart.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
buttonPauseStart = (Button) v;
if (buttonPauseStart.getText().equals("stop")) {
elapsedTime = System.currentTimeMillis() - startTime;
timerHandler.removeCallbacks(timerRunnable);
buttonPauseStart.setText("start");

} else {

startTime = System.currentTimeMillis() - elapsedTime;
timerHandler.postDelayed(timerRunnable, 0);
Calendar cs = Calendar.getInstance();
System.out.println("Current time => " + cs.getTime());

SimpleDateFormat df = new SimpleDateFormat("HH:mm");
String formattedDate = df.format(cs.getTime());

textTimeStarted.setText(formattedDate);
buttonPauseStart.setText("stop");
}
}
});

关于java - 暂停和恢复计时器而不重置计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27471015/

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