嘿傢伙我需要datepicker對話框的幫助,因爲我將它的值保存到onDateSet中的變量中:「selectedYear」,「selectedMonth」,「selectedDay」 這些是聲明的變量作爲班上的私人。我的問題是格式化日期格式化日期函數後,變量的值回到空,這就是爲什麼我創建了一個新的函數,我可以保存它們,我知道這是一個混亂的舉動,我想知道爲什麼變量變空了,這個代碼有什麼更好的解決方法。在先進的感謝datepicker對話框的值變空
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
selectedYear = year;
selectedMonth = monthOfYear;
selectedDay = dayOfMonth;
setDateEdit.setText(formatDate(selectedYear, selectedMonth, selectedDay));
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
private String formatDate(int year, int month, int day) {
saveDateToVar(year,month,day);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, month, day);
Date date = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
return sdf.format(date);
}
private void saveDateToVar(int year, int month, int day){
savedYear = year;
savedDay = day;
savedMonth = month;
}
您可以使用http://stackoverflow.com/questions/32960426/datepicker-dialog-box-does-not-open-on-click/32961832#32961832查看日期和時間的piker對話框。 – Sam