将倒计时设置为特定时间
Setting a countdown to a specific time
我正在尝试将倒计时设置为周二 19:00。我有开关盒来找日子。但我坚持进行倒计时。
我会将倒计时设置到我的 textField "daag"
请帮忙,谢谢
//To create switch case
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
//Today
final Calendar Today = Calendar.getInstance();
//Target day's hour
final Calendar tar = new GregorianCalendar();
tar.add(Calendar.HOUR_OF_DAY, 19);
final ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1);
switch (day) {
case Calendar.MONDAY:
daag.setText("Mandag");
break;
case Calendar.TUESDAY:
service.schedule(new Runnable() {
@Override
public void run () {
long diff = tar.getTimeInMillis() - Today.getTimeInMillis();
long diffSec = diff / 1000;
if (diff > 0) {
daag.setText("" + service.schedule(this, 1, TimeUnit.SECONDS);
}
}
}, 1, TimeUnit.SECONDS);
daag.setText("Tirsdag");
break;
试试这个
new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("done!");
}
}
.start();
参考这个 URL
我正在尝试将倒计时设置为周二 19:00。我有开关盒来找日子。但我坚持进行倒计时。 我会将倒计时设置到我的 textField "daag"
请帮忙,谢谢
//To create switch case
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
//Today
final Calendar Today = Calendar.getInstance();
//Target day's hour
final Calendar tar = new GregorianCalendar();
tar.add(Calendar.HOUR_OF_DAY, 19);
final ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1);
switch (day) {
case Calendar.MONDAY:
daag.setText("Mandag");
break;
case Calendar.TUESDAY:
service.schedule(new Runnable() {
@Override
public void run () {
long diff = tar.getTimeInMillis() - Today.getTimeInMillis();
long diffSec = diff / 1000;
if (diff > 0) {
daag.setText("" + service.schedule(this, 1, TimeUnit.SECONDS);
}
}
}, 1, TimeUnit.SECONDS);
daag.setText("Tirsdag");
break;
试试这个
new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("done!");
}
}
.start();
参考这个 URL