问题
I'm new here and new to Android Development, I Have a question regarding the Android CountDownTimer. How am I able to use Android's CountDownTimer, to count down to a specific date, let's say the 6th Novemeber 2015? Also How would I use the Java code with corresponding XML Code?
I had a llok at this question (how to countdown to a date) and couldn't really understand what was going on? Can anyone spare a moment to help me?
Thanks
回答1:
This is exactly how you do it
UPDATE 3
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
TextView tv_countdown = (TextView) findViewById(R.id.your_textview_id);
Calendar start_calendar = Calendar.getInstance();
Calendar end_calendar = Calendar.getInstance();
long start_millis = start_calendar.getTimeInMillis(); //get the start time in milliseconds
long end_millis = end_calendar.getTimeInMillis(); //get the end time in milliseconds
long total_millis = (end_millis - start_millis); //total time in milliseconds
end_calendar.set(2015, 10, 6); // 10 = November, month start at 0 = January
//1000 = 1 second interval
CountDownTimer cdt = new CountDownTimer(total_millis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long days = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
millisUntilFinished -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
millisUntilFinished -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
millisUntilFinished -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
tv_countdown.setText(days + ":" + hours + ":" + minutes + ":" + seconds); //You can compute the millisUntilFinished on hours/minutes/seconds
}
@Override
public void onFinish() {
tv_countdown.setText("Finish!");
}
};
cdt.start();
}
回答2:
Helpful method !
@Override
public void start(final long miliSecond, final int interval) {
new CountDownTimer(miliSecond, interval) {
@Override
public void onTick(long millisUntilFinished) {
long day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
millisUntilFinished -= TimeUnit.DAYS.toMillis(day);
long hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
millisUntilFinished -= TimeUnit.HOURS.toMillis(hour);
long minute = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
millisUntilFinished -= TimeUnit.MINUTES.toMillis(minute);
long second = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
intDay.setText("" + day);
intHour.setText("" + hour);
intMinute.setText("" + minute);
intSecond.setText("" + second);
}
@Override
public void onFinish() {
// What ever you want !
}
}.start();
}
回答3:
Date currentDate=new Date();
Date endDate=new Date();
long difference = endDate.getTime() - currentDate.getTime();
new CountDownTimer(difference, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
System.out.println("Done");
}
}.start();
来源:https://stackoverflow.com/questions/32773659/how-to-countdown-to-a-day-using-android-countdowntimer