我有一个android应用程序,这个应用程序有一个片段。我想在侧面片段中显示一个计时器倒计时。我该怎么做呢。定时器倒计时格式为YYYY-MM-DD。计时器结束后,计时器可见性将消失。我在我的应用程序活动上使用这个倒计时计时器,并且我想在fragment上使用它
谢谢..
public void countDownStart() {
handler = new android.os.Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
// Please here set your event date//YYYY-MM-DD
Date futureDate = dateFormat.parse("2018-11-4");
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
txtDay.setText("" + String.format("%02d", days));
txtHour.setText("" + String.format("%02d", hours));
txtMinute.setText(""
+ String.format("%02d", minutes));
txtSecond.setText(""
+ String.format("%02d", seconds));
} else {
textViewGone();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 1 * 1000);
}
public void textViewGone() {
}` 发布于 2018-07-06 12:20:32
安卓有CountdownTimer,它让你更容易做倒计时计时器。您可以按照本教程进行操作:https://www.youtube.com/watch?v=5ChnwWSAN5E
https://stackoverflow.com/questions/51202211
复制相似问题