如何在 TextView 中显示倒数计时器?
How do I display countdown timer in TextView?
我有Calendar
个对象
Calendar cal = Calendar.getInstance();
int hh = cal.get(Calendar.HOUR);
int mm = cal.get(Calendar.MINUTE);
int ss = cal.get(Calendar.SECOND);
并格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("h:mm:s a", Locale.US);
我希望我的 TextView
以 hh:mm:ss
格式显示倒计时
Time left: 03:25:12
Time left: 03:25:11
Time left: 03:25:10
Time left: 03:25:09
.
.
. so on and stop the countdown after 3 hours, 25 minutes and 8 seconds.
时间过去后 TextView
应该显示 Time left: 00:00:00
。
使用计时器 class 作为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
long maxTimeInMilliseconds = 3*25*8*1000;// in your case
startTimer(maxTimeInMilliseconds, 1000);
}
public void startTimer(final long finish, long tick)
{
t = new CountDownTimer(finish, tick) {
public void onTick(long millisUntilFinished)
{
long remainedSecs = millisUntilFinished/1000;
timer.setText(""+(remainedSecs/60)+":"+(remainedSecs%60));// manage it accordign to you
}
public void onFinish()
{
timer.setText("00:00:00");
cancel();
}
}.start();
}
我有Calendar
个对象
Calendar cal = Calendar.getInstance();
int hh = cal.get(Calendar.HOUR);
int mm = cal.get(Calendar.MINUTE);
int ss = cal.get(Calendar.SECOND);
并格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("h:mm:s a", Locale.US);
我希望我的 TextView
以 hh:mm:ss
格式显示倒计时
Time left: 03:25:12
Time left: 03:25:11
Time left: 03:25:10
Time left: 03:25:09
.
.
. so on and stop the countdown after 3 hours, 25 minutes and 8 seconds.
时间过去后 TextView
应该显示 Time left: 00:00:00
。
使用计时器 class 作为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
long maxTimeInMilliseconds = 3*25*8*1000;// in your case
startTimer(maxTimeInMilliseconds, 1000);
}
public void startTimer(final long finish, long tick)
{
t = new CountDownTimer(finish, tick) {
public void onTick(long millisUntilFinished)
{
long remainedSecs = millisUntilFinished/1000;
timer.setText(""+(remainedSecs/60)+":"+(remainedSecs%60));// manage it accordign to you
}
public void onFinish()
{
timer.setText("00:00:00");
cancel();
}
}.start();
}