我想在android中爲遊戲/日期製作倒數計時器。我想創建一個計時器,將日期,小時,分鐘和秒顯示到我用最終變量指定的日期。計時器然後設置文本視圖以顯示用戶的日,小時,分鐘和秒。Android倒數計時器至今
有關我如何編碼的任何建議?
我想在android中爲遊戲/日期製作倒數計時器。我想創建一個計時器,將日期,小時,分鐘和秒顯示到我用最終變量指定的日期。計時器然後設置文本視圖以顯示用戶的日,小時,分鐘和秒。Android倒數計時器至今
有關我如何編碼的任何建議?
看看這篇文章,我認爲這將幫助你:How to check day in android?
使用java.util.Calendar類,我想你應該能夠找出如何做休息。你可以使用getDate(),getHours(),getMinutes(),getSeconds()等等......希望有幫助!
這個軟件是使用純代碼還是AppInventor?這裏是編碼資源。
這裏是一個資源: [http://w2davids.wordpress.com/simple-countdowntimer-example/][1]
第二(本地)資源: Countdown Timer required on Android
這裏是一個Android內置CountDownTimer,將顯示格式化你的天,小時,分鐘,並在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();
}
}
天數爲+1總是可以減少嗎? – 2012-11-20 09:48:09
@AbhayKumar我更新了我的代碼,並給了這些日子一個標籤。 – Sam 2012-11-20 21:25:59
這種方法在我的模擬器上每分鐘跳過3秒鐘。它不在我的手機上。這是一個模擬器問題,還是我應該期待在其他手機上發生? – 2013-08-02 00:00:04
CountDownTimer將顯示格式化爲小時,分鐘,天和秒的時間。
public class DemotimerActivity extends Activity {
/** Called when the activity is first created. */
TextView tv;
long diff;
long milliseconds;
long endTime;
@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");
formatter.setLenient(false);
String oldTime = "21.10.2013, 12:00";
Date oldDate;
try {
oldDate = formatter.parse(oldTime);
milliseconds = oldDate.getTime();
//long startTime = System.currentTimeMillis();
// do your work...
long endTime=System.currentTimeMillis();
diff = endTime-milliseconds;
Log.e("day", "miliday"+diff);
long seconds = (long) (diff/1000) % 60 ;
Log.e("secnd", "miliday"+seconds);
long minutes = (long) ((diff/(1000*60)) % 60);
Log.e("minute", "miliday"+minutes);
long hours = (long) ((diff/(1000*60*60)) % 24);
Log.e("hour", "miliday"+hours);
long days = (int)((diff/(1000*60*60*24)) % 365);
Log.e("days", "miliday"+days);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Long serverUptimeSeconds = (System.currentTimeMillis() - milliseconds)/1000;
String serverUptimeText = String.format("%d days %d hours %d minutes %d seconds",
serverUptimeSeconds/86400,
(serverUptimeSeconds % 86400)/3600 ,
((serverUptimeSeconds % 86400) % 3600)/60,
((serverUptimeSeconds % 86400) % 3600) % 60
);
Log.v("jjj", "miliday"+serverUptimeText);
MyCount counter = new MyCount(milliseconds,1000);
counter.start();
}
// countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
//tv.setText("Left: " + millisUntilFinished/1000);
long diff = endTime - millisUntilFinished;
Log.e("left", "miliday"+diff);
long seconds = (long) (diff/1000) % 60 ;
//Log.e("secnd", "miliday"+seconds);
long minutes = (long) ((diff/(1000*60)) % 60);
//Log.e("minute", "miliday"+minutes);
long hours = (long) ((diff/(1000*60*60)) % 24);
//Log.e("hour", "miliday"+hours);
int days = (int)((diff/(1000*60*60*24)) % 365);
Log.v("days", "miliday"+days);
Long serverUptimeSeconds =
(System.currentTimeMillis() - millisUntilFinished)/1000;
String serverUptimeText =
String.format("%d days %d hours %d minutes %d seconds",
serverUptimeSeconds/86400,
(serverUptimeSeconds % 86400)/3600 ,
((serverUptimeSeconds % 86400) % 3600)/60,
((serverUptimeSeconds % 86400) % 3600) % 60
);
Log.v("new its", "miliday"+serverUptimeText);
// tv.setText(days +":"+hours+":"+minutes + ":" + seconds);
tv.setText(serverUptimeText);
}
}
}
它實際上增加了時間計數器而不是減法計數器。 – CoDe 2014-11-13 17:21:08
試試這個:
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();
}
這應該被接受 – Dhiru 2017-06-23 07:37:42
@Dhiru我同意。這一個工程 – 2017-09-12 14:50:35
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();
}
http://joda-time.sourceforge.net/faq.html#datediff – iTurki 2012-08-09 01:38:58