我需要在倒数计时器上每秒更新一次,倒计时到圣诞节。 如何在文本视图中更新时间。我没有onCreate方法中的代码。
这是我的一些代码。当我运行代码时,它会给出确切的时间,但它不会在屏幕上实时倒计时。有任何想法吗? 编辑:readThisPeriod用于计算圣诞节前的剩余时间。
public void DateCalculator() {
Calendar today = Calendar.getInstance();
Calendar thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH, 25);
thatDay.set(Calendar.MONTH, 11); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2014);
thatDay.set(Calendar.HOUR, 0);
thatDay.set(Calendar.MINUTE, 0);
thatDay.set(Calendar.SECOND, 0);
thatDay.set(Calendar.AM_PM, 0);
System.out.println(thatDay.getTime());
ScheduledExecutorService scheduledExecutorService =
Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleAtFixedRate(new ReadThisPeriod(thatDay), 0, 1,
TimeUnit.SECONDS);
long diff = (thatDay.getTimeInMillis() - today.getTimeInMillis()) / 1000;
long days = diff / (60 * 60 * 24);
long hours = diff / (60 * 60) % 24;
long minutes = diff / 60 % 60;
long seconds = diff % 60;
daysBox = (TextView) findViewById(R.id.s1Days);
daysBox.setText("" + "" + days + "" + hours + "" + minutes + "" + seconds + " );
}
答案 0 :(得分:1)
开始CountDownTimer
:
long endTime = thatDay.getTimeInMillis();
new MyCountDownTimer(endTime-System.currentTimeMillis(),1000).start();
你走了:
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
textView.setText("Happy Christmas!");
}
@Override
public void onTick(long millis) {
int SECOND = 1000;
int MINUTE = 60 * SECOND;
int HOUR = 60 * MINUTE;
int DAY = 24 * HOUR;
StringBuffer text = new StringBuffer();
if (millis > DAY) {
text.append(millis / DAY).append(" days ");
millis %= DAY;
}
if (millis > HOUR) {
text.append(millis / HOUR).append(" hours ");
millis %= HOUR;
}
if (millis > MINUTE) {
text.append(millis / MINUTE).append(" minutes ");
millis %= MINUTE;
}
if (millis > SECOND) {
text.append(millis / SECOND).append(" seconds ");
millis %= SECOND;
}
textView.setText(text);
}
}
答案 1 :(得分:0)
import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends Activity {
private TextView daysBox;
private boolean running;
private Calendar thatDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
daysBox = (TextView) findViewById(R.id.main_day);
thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH, 25);
thatDay.set(Calendar.MONTH, 11); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2014);
thatDay.set(Calendar.HOUR, 0);
thatDay.set(Calendar.MINUTE, 0);
thatDay.set(Calendar.SECOND, 0);
thatDay.set(Calendar.AM_PM, 0);
startDateCalculator();
}
protected void onPause() {
super.onPause();
running = false;
}
protected void onResume() {
super.onResume();
running = true;
}
private class CountdownTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... args) {
while (running) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
publishProgress();
}
return null;
}
protected void onProgressUpdate(Void... res) {
super.onProgressUpdate(res);
long diff = (thatDay.getTimeInMillis() - System.currentTimeMillis()) / 1000;
long days = diff / (60 * 60 * 24);
long hours = diff / (60 * 60) % 24;
long minutes = diff / 60 % 60;
long seconds = diff % 60;
daysBox.setText("" + "" + days + "d " + hours + ":" + minutes + ":" + seconds + " ");
}
}
public void startDateCalculator() {
new CountdownTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
答案 2 :(得分:0)
long diff = (thatDay.getTimeInMillis() - today.getTimeInMillis()) / 1000;
long total=diff;
mTimer=new CountDownTimer(total, 1000) {
public void onTick(long millisUntilFinished) {
diff--;
daysBox.setText(convertSecondToHHMMString(diff));
}
public void onFinish() {
System.out.println("Happy Chrismas");
}
}.start();
private String convertSecondToHHMMString(long secondtTime){
TimeZone tz = TimeZone.getTimeZone("UTC");
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(tz);
String time = df.format(new Date(secondtTime*1000L));
return time;
}