我有一个DatePicker,我禁用了SpinnerView,现在只使用CalendarView。但它从星期日开始,我想使用星期一作为一周的第一天。我怎么能改变呢?我猜测第一天将取决于手机的设置,但在我的瑞士手机上仍然是错误的......
我没有找到任何set Method或任何XML命令。
<DatePicker
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
和
DatePicker datePicker = (DatePicker) convertView.findViewById(R.id.date_picker);
datePicker.setSpinnersShown(false);
------------编辑:------------
我尝试更改区域设置:
Locale.setDefault(Locale.GERMAN);
但这并没有改变任何以太。所以我读了android-17.android.widget.DatePicker.java,我发现了:
public DatePicker(Context context, AttributeSet attrs, int defStyle) {
//...
// initialization based on locale
setCurrentLocale(Locale.getDefault());
//...
}
/**
* Sets the current locale.
*
* @param locale The current locale.
*/
private void setCurrentLocale(Locale locale) {
if (locale.equals(mCurrentLocale)) {
return;
}
mCurrentLocale = locale;
mTempDate = getCalendarForLocale(mTempDate, locale);
mMinDate = getCalendarForLocale(mMinDate, locale);
mMaxDate = getCalendarForLocale(mMaxDate, locale);
mCurrentDate = getCalendarForLocale(mCurrentDate, locale);
mNumberOfMonths = mTempDate.getActualMaximum(Calendar.MONTH) + 1;
mShortMonths = new String[mNumberOfMonths];
for (int i = 0; i < mNumberOfMonths; i++) {
mShortMonths[i] = DateUtils.getMonthString(Calendar.JANUARY + i,
DateUtils.LENGTH_MEDIUM);
}
}
/**
* Gets a calendar for locale bootstrapped with the value of a given calendar.
*
* @param oldCalendar The old calendar.
* @param locale The locale.
*/
private Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
if (oldCalendar == null) {
return Calendar.getInstance(locale);
} else {
final long currentTimeMillis = oldCalendar.getTimeInMillis();
Calendar newCalendar = Calendar.getInstance(locale);
newCalendar.setTimeInMillis(currentTimeMillis);
return newCalendar;
}
}
那么为什么Locale.setDefault(Locale.GERMAN)不起作用?! 正如您在上面看到的,我从XML中获取了DatePicker ..是否有任何错误?!
答案 0 :(得分:13)
您必须手动设置一周的第一天,例如在您的活动/片段的onCreate()/ onCreateView()中:
date = (DatePicker) value.findViewById(R.id.date);
if (Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1) {
date.setSpinnersShown(false);
date.setCalendarViewShown(true);
date.getCalendarView().setFirstDayOfWeek(Calendar.getInstance().getFirstDayOfWeek());
}
上面的示例使用默认语言环境(Calendar.getInstance()实际执行),但您可以手动指定日期:
date.getCalendarView().setFirstDayOfWeek(Calendar.MONDAY);
答案 1 :(得分:8)
使用getCalendarView()
的当前接受的答案已弃用材质样式。
但是,您可以在setFirstDayOfWeek()
实例上调用DatePicker
。或者如果您正在开始DatePickerDialog
:
DatePickerDialog datePickerDlg = new DatePickerDialog(...);
datePickerDlg.getDatePicker().setFirstDayOfWeek(Calendar.MONDAY);
datePickerDlg.show();
答案 2 :(得分:0)
这取决于您设备的本地化。
看看这里:How to make GWT DatePicker to use Monday as the first day of the week?