可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to make a countdown timer for a game/date in android. I want to create a timer that displays the days, hours, minutes, and seconds to a date I specify with a final variable. The timer then sets text views to show the days, hours, minutes, and seconds to the user.
Any suggestions about how I could code this?
回答1:
CountDownTimer that will display the time formatted to hours,minute,days,and seconds.
public class DemotimerActivity extends Activity {
/** Called when the activity is first created. */
TextView tv;
long diff;
long oldLong;
long NewLong;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
String oldTime = "19.02.2018, 12:00";//Timer date 1
String NewTime = "20.02.2018, 14:00";//Timer date 2
Date oldDate, newDate;
try {
oldDate = formatter.parse(oldTime);
newDate = formatter.parse(NewTime);
oldLong = oldDate.getTime();
NewLong = newDate.getTime();
diff = NewLong - oldLong;
} catch (ParseException e) {
e.printStackTrace();
}
MyCount counter = new MyCount(diff, 1000);
counter.start();
}
// countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
txtNumber1.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = (TimeUnit.MILLISECONDS.toDays(millis)) + "Day "
+ (TimeUnit.MILLISECONDS.toHours(millis) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis)) + ":")
+ (TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)) + ":"
+ (TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
txtNumber1.setText(/*context.getString(R.string.ends_in) + " " +*/ hms);
}
}
}
回答2:
Try this one:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
formatter.setLenient(false);
String endTime = "25.06.2017, 15:05:36"
Date endDate;
try {
endDate = formatter.parse(endTime);
milliseconds = endDate.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startTime = System.currentTimeMillis();
diff = milliseconds - startTime;
mCountDownTimer = new CountDownTimer(milliseconds, 1000) {
@Override
public void onTick(long millisUntilFinished) {
startTime=startTime-1;
Long serverUptimeSeconds =
(millisUntilFinished - startTime) / 1000;
String daysLeft = String.format("%d", serverUptimeSeconds / 86400);
txtViewDays.setText(daysLeft);
String hoursLeft = String.format("%d", (serverUptimeSeconds % 86400) / 3600);
txtViewHours.setText(hoursLeft);
String minutesLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60);
txtViewMinutes.setText(minutesLeft);
String secondsLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60);
txtViewSecond.setText(secondsLeft);
}
@Override
public void onFinish() {
}
}.start();
}
回答3:
Here is an Android built-in CountDownTimer that will display the time formatted to your days, hours, minutes, and seconds all in a TextView:
public class Example extends Activity {
CountDownTimer mCountDownTimer;
long mInitialTime = DateUtils.DAY_IN_MILLIS * 2 +
DateUtils.HOUR_IN_MILLIS * 9 +
DateUtils.MINUTE_IN_MILLIS * 3 +
DateUtils.SECOND_IN_MILLIS * 42;
TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.empty);
mCountDownTimer = new CountDownTimer(mInitialTime, 1000) {
StringBuilder time = new StringBuilder();
@Override
public void onFinish() {
mTextView.setText(DateUtils.formatElapsedTime(0));
//mTextView.setText("Times Up!");
}
@Override
public void onTick(long millisUntilFinished) {
time.setLength(0);
// Use days if appropriate
if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
if(count > 1)
time.append(count).append(" days ");
else
time.append(count).append(" day ");
millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
}
time.append(DateUtils.formatElapsedTime(Math.round(millisUntilFinished / 1000d)));
mTextView.setText(time.toString());
}
}.start();
}
}
回答4:
Take a look at this post, I think it will help you out: How to check day in android?
Using the java.util.Calendar class, I think you should be able to figure out how to do the rest.
http://developer.android.com/reference/java/util/Date.html -- you can use the getDate(), getHours(), getMinutes(), getSeconds(), etc... Hope that helps!
回答5:
Is this software your using pure code or AppInventor? Here's coding resources.
Here's one resource:
[http://w2davids.wordpress.com/simple-countdowntimer-example/][1]
A second (local) resource:
Countdown Timer required on Android
回答6:
long startTime;
private void start_countdown_timer()
{
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
formatter.setLenient(false);
String endTime = "18.09.2017, 15:05:36";
long milliseconds=0;
final CountDownTimer mCountDownTimer;
Date endDate;
try {
endDate = formatter.parse(endTime);
milliseconds = endDate.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startTime = System.currentTimeMillis();
mCountDownTimer = new CountDownTimer(milliseconds, 1000) {
@Override
public void onTick(long millisUntilFinished) {
startTime=startTime-1;
Long serverUptimeSeconds =
(millisUntilFinished - startTime) / 1000;
String daysLeft = String.format("%d", serverUptimeSeconds / 86400);
//txtViewDays.setText(daysLeft);
Log.d("daysLeft",daysLeft);
String hoursLeft = String.format("%d", (serverUptimeSeconds % 86400) / 3600);
//txtViewHours.setText(hoursLeft);
Log.d("hoursLeft",hoursLeft);
String minutesLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60);
//txtViewMinutes.setText(minutesLeft);
Log.d("minutesLeft",minutesLeft);
String secondsLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60);
//txtViewSecond.setText(secondsLeft);
Log.d("secondsLeft",secondsLeft);
}
@Override
public void onFinish() {
}
}.start();
}