我已按如下方式实施了日期选择器:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month, day);
}
return null;
}
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) {
year = selectedYear;
month = selectedMonth;
String strMonth = "";
if (month == 0) {
strMonth = "January";
} else if (month == 1) {
strMonth = "February";
} else if (month == 2) {
strMonth = "March";
} else if (month == 3) {
strMonth = "April";
} else if (month == 4) {
strMonth = "May";
} else if (month == 5) {
strMonth = "June";
} else if (month == 6) {
strMonth = "July";
} else if (month == 7) {
strMonth = "August";
} else if (month == 8) {
strMonth = "September";
} else if (month == 9) {
strMonth = "October";
} else if (month == 10) {
strMonth = "November";
} else if (month == 11) {
strMonth = "December";
}
day = selectedDay;
tvPurchaseDate.setText(new StringBuilder().append(day).append(" ").append(strMonth).append(" ").append(year).append(" "));
tvPurchaseDate.setTextColor(Color.BLACK);
}
};
我遇到的问题是日期选择器显示错误的日期,如下图所示。
创建对话框时:
如果我选择其他日期:
请注意,日期不是当前日期(今天)和车轮上显示的年份,并且日期选择器顶部显示的年份完全不同。我做错了吗?我不明白,因为我以前使用过这段代码而且工作正常。从视图鳍状肢中调用日期选择器。提前谢谢
修改
这就是我调用日期选择器的方式:
private int day, month, year;
在我的onClick中:
showDialog(DATE_DIALOG_ID);
然后调用onCreateDialog()(显示在上面的代码中),所有这些代码都在调用活动的内部。
答案 0 :(得分:0)
刚刚解决了这个问题,我需要将日期设置为今天,因为日期选择器只添加了它可以找到的第一个日期,因此所有内容都显示错误。在我的onCreate()
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);