[英]How do I get Chronometer to work as a countdown timer in Android?
I am making an app in android studio, and I want to be able to display the amount of time left until a certain time in the day. 我正在android studio中制作一个应用程序,我希望能够显示直到一天中的某个时间为止的剩余时间。 For some reason I can't seem to get my code to work; 由于某种原因,我似乎无法使我的代码正常工作。 when I run my code does anyone know what I am doing wrong? 当我运行代码时,有人知道我在做什么错吗?
mChronometer = (Chronometer) findViewById(R.id.end_chronometer);
mChronometer.setCountDown(true);
mChronometer.setBase(convertToDate(timeStr));
...
private long convertToDate(String str)
{
try
{
DateFormat mDateTimeFormat = new SimpleDateFormat("HH:MM");
Date date = mDateTimeFormat.parse(str);
} catch (java.text.ParseException e)
{
e.printStackTrace();
}
return SystemClock.elapsedRealtime();
}
you can use CountDownTimer
. 您可以使用CountDownTimer
。
new CountDownTimer((hourOfDay * 60 + minute1) * 60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
SimpleDateFormat mTimeFormat = new SimpleDateFormat("mm:ss");
mTvRemainTime.setText(mTimeFormat.format(new Date(millisUntilFinished)));
}
@Override
public void onFinish() {
mTvRemainTime.setText("stop");
}
}.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.