我需要在我的應用程序中使用日期選擇器對話框。首先,我打開日期選擇器對話框,然後選擇日期。下次每次打開Date pickerDialog時,日期選擇器的日期應該是設備的當前日期。但是,它會在對話框中顯示上次選擇的日期。請幫我設置代碼。這裏是我的代碼,因爲我有現在:在android中設置DatePicker日期
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
Date d=new Date();
Calendar c=Calendar.getInstance();
c.setTimeZone(tz);
c.setTime(d);
d=c.getTime();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
DatePickerDialog datePicker
= new DatePickerDialog(this, datePickerListener, year, month, date);
return datePicker;
}
return super.onCreateDialog(id);
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener()
{
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay)
{
cors_year = selectedYear;
cors_month = selectedMonth;
cors_date = selectedDay;
getDate("calledfromDialog");
displaySunTime();
displayChoghadiya("Day");
displayTime(3, 1);
dayButtonClicked();
selected_DAY_NIGHT="Day";
}
};
你有沒有嘗試過這些'c.setTimeZone(tz); c.setTime(d);'兩行? – Shoshi 2013-03-08 18:48:14
它需要根據當前默認時區獲取當前日期。請忽略它 – 2013-03-09 07:07:55