如何在Android应用程序中倒计时?

时间:2014-04-05 11:36:17

标签: android countdown days

最后一天,我试图创建一个倒计时77天。我做到了,但问题是我的倒计时工作时间为毫秒,77天(毫秒)正好是6652800000,问题是如果我用这个值设置我的倒计时,它会显示一个错误,上面写着:字面值6652800000 type int超出范围。如何正确设置倒计时从现在到将来的77天倒计时?谢谢!

这是我的:

MainActivity:

package com.example.dasds;


import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;


public class MainActivity extends Activity {
TextView tv; //textview to display the countdown
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
//5000 is the starting number (in milliseconds)
//1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(6652800000,86400000);
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/86400000);
}
}
}

4 个答案:

答案 0 :(得分:1)

在数字文字后面输入L,将其投放到long

MyCount counter = new MyCount(6652800000L,86400000L);

答案 1 :(得分:0)

int year = Integer.parseInt(year);
int month = Integer.parseInt(monthafter77days);
int day = 
Integer.parseInt(dateafter77days);
int hour = Integer.parseInt(hourneeded if );
int minute = Integer.parseInt(minuteneeded if);
GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute);
/**
* Converting the date and time in to
* milliseconds elapsed since epoch
*/
long alarm_time = calendar.getTimeInMillis();
schedulealarm(alarm_time );

享受

答案 2 :(得分:0)

尝试在调用MYCount构造函数时将参数值转换为long。

答案 3 :(得分:0)

尝试按如下方式更改onTick方法。当传递双值和长值时,Android文本视图往往会混乱。

 public void onTick(long millisUntilFinished) {
    tv.setText("Left: " + String.valueof(millisUntilFinished/86400000));
}