我是 Java
和 Android
的新手,我想在我的 ACTION_UP
事件中设置一个计时器并取消计时器而我做其他 Activity 。我如何才能基本上为此设置一个计时器并停止并重置其他事件的计时器?
最佳答案
对于 CountDownTimer
在这里,我已经开始了 30 秒的计时
CountDownTimer countDownTimer;
TextView tvTicker = (TextView) findViewById(R.id.tvTicker);
public void startClicked(View view) { //When button start is clicked
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
tvTicker.setText("seconds remaining: " + millisUntilFinished / 1000);
//Do some stuff here for saving the duration to a variable or anything else as your requirements
}
public void onFinish() {
tvTicker.setText("done!");
}
}.start();
}
方法说明
CountDownTimer(long millisInFuture, long countDownInterval)
millisInFuture
= 时间以毫秒为单位
countDownInterval
= 间隔以毫秒为单位
现在您可以使用这些方法进行其他类型的操作。
countDownTimer.cancel(); //Cancel the countdown.
countDownTimer.onFinish() //Callback fired when the time is up.
countDownTimer.onTick(long millisUntilFinished); //Callback fired on regular `interval. millisUntilFinished is The amount of time until finished.`
关于android - 单击按钮后如何查看持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34281158/