我正在尝试根据用户输入的日期在我的 android 应用程序中设置倒计时。
因此,用户有一个日期选择器,它在 EditText 对象中设置日期,然后当我单击保存时,我需要它从 EditText 对象中获取值并运行以下代码:
final EditText dateField = (EditText) findViewById(R.id.dateSentData);
String dateSent = dateField.getText().toString();
Double date = Double.parseDouble(dateSent);
Time TimerSet = new Time();
TimerSet.set(date); //day month year
TimerSet.normalize(true);
long millis = TimerSet.toMillis(true);
Time TimeNow = new Time();
TimeNow.setToNow(); // set the date to Current Time
TimeNow.normalize(true);
long millis2 = TimeNow.toMillis(true);
long millisset = millis - millis2; //subtract current from future to set the time remaining
final int smillis = (int) (millis); //convert long to integer to display conversion results
final int smillis2 = (int) (millis2);
new CountDownTimer(millisset, 1000) {
public void onTick(long millisUntilFinished) {
mText = (TextView)findViewById(R.id.weddingDateData);
// decompose difference into days, hours, minutes and seconds
int weeks = (int) ((millisUntilFinished / 1000) /
604800);
int days = (int) ((millisUntilFinished / 1000) / 86400);
int hours = (int) (((millisUntilFinished / 1000) - (days
* 86400)) / 3600);
int minutes = (int) (((millisUntilFinished / 1000) - ((days
* 86400) + (hours * 3600))) / 60);
int seconds = (int) ((millisUntilFinished / 1000) % 60);
int millicn = (int) (millisUntilFinished / 1000);
mText.setText(" " +days);
}
public void onFinish() {
}
}.start();
上面的代码不会运行,因为它在下面的行中除了 String 或 Double 只有 Long:
TimerSet.set(date);
有人可以帮我吗?
提前致谢