我的活动中有一个倒数计时器。 我将分钟转换成毫秒。
基于这一毫秒,我已经成功地使用CountDownTimer对 HH:mm:ss 进行了计数。
所以,我要区分主要时间和剩余时间。
例如,我的主要时间是 00:10:00 ,而当我停下柜台时,我的剩余时间是 00:07:30 。表示我花了 2:30 秒
我想要的预期结果是 150秒。
示例代码:
public class MainActivity extends AppCompatActivity {
int duration;
TextView textView;
ProgressBar progressbar;
Button btn_stop, btn_resume;
CountDownTimer counter;
int i = 1;
int remainingDuration;
int countDownInterval = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tvSampleText);
progressbar = (ProgressBar) findViewById(R.id.progressbar);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_resume = (Button) findViewById(R.id.btn_resume);
// duration=81200000; //6 hours
duration = (int) TimeUnit.MINUTES.toMillis(1);
counter = new MyCount(duration, countDownInterval);
counter.start();
btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter.cancel();
}
});
btn_resume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter = new MyCount(remainingDuration, countDownInterval);
counter.start();
}
});
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
textView.setText("00:00:00");
}
@Override
public void onTick(long millisUntilFinished) {
i++;
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long elapsedHours = millisUntilFinished / hoursInMilli;
millisUntilFinished = millisUntilFinished % hoursInMilli;
long elapsedMinutes = millisUntilFinished / minutesInMilli;
millisUntilFinished = millisUntilFinished % minutesInMilli;
long elapsedSeconds = millisUntilFinished / secondsInMilli;
String yy = String.format("%02d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds);
textView.setText(yy);
progressbar.setProgress((int) i * 100 / (duration / (int) secondsInMilli));
remainingDuration = (int) millisUntilFinished;
}
}
}
答案 0 :(得分:2)
首先将分钟持续时间转换为秒数
long secs = minutes * 60; 10 * 60 = 600
在滴答声中,您会得到剩余时间(以毫秒为单位),因此请将该值保留为全局参考,并将其转换为秒为
long mMillisUntilFinished;
void onTick (long millisUntilFinished){
// mMillisUntilFinished = 450000
mMillisUntilFinished = millisUntilFinished;
}
然后按停止键,然后将重命名时间差设为
long diffSeconds = secs - (mMillisUntilFinished / 1000)
// 600 - (450000 /1000)
// 600 - 450
// 150
更新:按照建议,计算持续时间和剩余时间的秒数,然后减去它们以获得结果。
使用long
代替int
投射
@Override
public void onTick(long millisUntilFinished) {
i++;
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long tempMili = millisUntilFinished;
long elapsedHours = tempMili / hoursInMilli;
tempMili = tempMili % hoursInMilli;
long elapsedMinutes = tempMili / minutesInMilli;
tempMili = tempMili % minutesInMilli;
long elapsedSeconds = tempMili / secondsInMilli;
String yy = String.format("%02d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds);
textView.setText(yy);
progressbar.setProgress((int) i * 100 / (duration / (int) secondsInMilli));
remainingDuration = (duration/ 1000) - (millisUntilFinished/1000);
}