作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我按下按钮以显示 DatePickerDialog 时,对话框显示多一个月。例如,如果我像这样以当前日期开始(使用 joda 库的 DateTime):
DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
DateTime dt = new DateTime(zone);
int year = dt.getYear();
int month = dt.getMonthOfYear();
int day = dt.getDayOfMonth();
即 07/08/2014,日期对话框显示比 07/09/2014 大一个月。我不明白为什么会这样。
代表 datePickerFragment 的 fragment 是:
@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@SuppressLint("ValidFragment")
TextView txtDate;
GlobalData appState;
public DatePickerFragment(TextView txtDate) {
super();
this.txtDate = txtDate;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
DateTime dt = new DateTime(zone);
int year = dt.getYear();
int month = dt.getMonthOfYear();
int day = dt.getDayOfMonth();
Log.i("DatePickerFragment day month year", day +" "+ month + " "+ year + "");
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
appState.setDateUserFrom(year, month, day);
Log.i("Date day month yerar", "Date changed." + day+" " + month + " " +year);
txtDate.setText(new StringBuilder().append(day)
.append("-").append(month).append("-").append(year)
.append(" "));
}
}
最佳答案
DatePickerDialog采用 monthOfYear
即 0 到 11
[1 月为 0...Dec 为 11],并且您的 DateTime
返回 1 到 12
。所以你需要用月值做 -1
。
使用这个:
return new DatePickerDialog(getActivity(), this, year, month - 1, day);
关于android - DatePickeDialog 在 android 中显示多一个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25183400/
当我按下按钮以显示 DatePickerDialog 时,对话框显示多一个月。例如,如果我像这样以当前日期开始(使用 joda 库的 DateTime): DateTimeZone zone =
我是一名优秀的程序员,十分优秀!