gpt4 book ai didi

android - 在 DatePicker 中将一周的第一天设置为星期一

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:24 25 4
gpt4 key购买 nike

我有一个 DatePicker,我在其中禁用了 SpinnerView,现在只使用 CalendarView。但它从星期日开始,我想用星期一作为一周的第一天。我该如何改变它?我猜第一天将取决于手机设置,但在我的瑞士手机上它仍然是错误的......

我还没有找到任何设置方法或任何 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。有什么错误吗?!

最佳答案

您必须手动设置一周的第一天,例如在您的 Activity/fragment 的 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);

关于android - 在 DatePicker 中将一周的第一天设置为星期一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13631244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com