Android:如何将 DatePicker 视图日期格式从 MM/dd/yyyy 更改为 dd/MM/yyyy?

发布于 2024-12-01 12:26:05 字数 454 浏览 1 评论 0原文

我很长一段时间以来一直试图找到这个问题的答案。我还查看了以下链接:Android - DatePicker Widget Format。 在我的手机/模拟器 (v 2.3.3) 上,DatePicker 小部件和对话框均不采用设置中指定的日期格式(设置 -> 日期和时间 -> 选择日期格式)。它们的格式始终为“MM/dd/yyyy”。区域设置设置为英国英语。

当我读取设置时,我得到“d/MM/yyyy”:

Settings.System.getString(getContentResolver(), Settings.System.DATE_FORMAT);

有人知道如何更改DatePicker 视图中的日期格式?

I have been trying to get an answer to this question for quite a while. I also had a look at the following link: Android - DatePicker Widget Format.
On my phone/ emulator (v 2.3.3) both the DatePicker widget and the dialog do not take the date format specified in settings (Settings->Date&Time->Select date format). They are always in the format "MM/dd/yyyy". The locale is set to English UK.

I get "d/MM/yyyy" when I read the settings with:

Settings.System.getString(getContentResolver(), Settings.System.DATE_FORMAT);

Does anybody have an idea about how to change the date format in a DatePicker view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-12-08 12:26:06

该设置非常具体。它仅适用于以“短格式”显示的日期。只要您使用其他任何内容(在本例中,DatePicker 似乎使用“中”),那么这些设置将被忽略,并且区域设置的默认格式将接管。

我可以确认,如果您更改手机的语言(实际上是区域设置),日期选择器中元素的顺序确实会发生变化。我想这意味着我不同意 Dan S 的说法,即“系统不使用任何机制来本地化 DatePicker”。如果你看一下代码,你可以看到(至少从 2.1 开始),有一个 reorderPickers() 方法正是执行此操作。

英语:
English

法语:
法语

That setting is very specific. It is only meant to apply to dates dates shown in "short format". As long as you're using anything else (in this case, the DatePicker seems to be using "medium"), then those settings will be disregarded and the Locale's default formatting will take over.

I can confirm that if you change your phone's the language (and, effectively, the Locale) the order of the elements does changes in the date picker. Which I guess means I disagree with Dan S's assertion that "the system does not use any mechanism to localize the DatePicker". If you look at the code, you can see that (at least since 2.1), there's a reorderPickers() method that does exactly this.

English:
English

French:
French

人生戏 2024-12-08 12:26:05

要覆盖 DatePicker 格式,最好的选择是通过对原始代码进行轻微编辑来覆盖其构造函数之一。您可以在代码中为您编辑 ViewGroup,但我会认真考虑不要这样做。

  1. 创建类 LocalizedDatePicker
  2. 覆盖 public DatePicker(上下文上下文,AttributeSet attrs,int defStyle) http://developer.android.com/reference/android /widget/DatePicker.html#DatePicker(android.content.Context, android.util.AttributeSet, int)
  3. 复制 原始 DatePicker 构造函数(您经过一些测试后可能需要选择不同的分支,我已经链接了头)
  4. 覆盖调用 reorderSpinners() 的方法以删除该调用
  5. 。 this, true); with inflater.inflate(my.package.R.layout.localized_date_picker, this, true);
  6. 将原始 date_picker.xml 布局复制到本地资源 localized_date_picker.xml
  7. 根据所需的顺序编辑文件,因为您正在编辑此文件,所以请尊重其他地方的本地化,并在全局布局文件夹中保留原始副本,并将 localized_date_picker.xml 放在您所在区域的特定位置 文件夹。由于系统的布局是月日年,其他地方的人可能会期望这样的顺序。

To override the DatePicker format the best bet would be to override one of its constructors with a slight edit to the original code. You can edit the ViewGroup in code for you but I'd seriously consider not doing it that way.

  1. Create class LocalizedDatePicker
  2. Override public DatePicker (Context context, AttributeSet attrs, int defStyle) http://developer.android.com/reference/android/widget/DatePicker.html#DatePicker(android.content.Context, android.util.AttributeSet, int)
  3. Copy the code from the original DatePicker constructor (you may need to select a different branch after some testing, I have linked the head)
  4. Override methods that call reorderSpinners() to remove that call.
  5. Replace line inflater.inflate(R.layout.date_picker, this, true); with inflater.inflate(my.package.R.layout.localized_date_picker, this, true);
  6. Copy the original date_picker.xml layout to your local resource localized_date_picker.xml
  7. Edit the file for the desired order, since you're editing this please respect the localization of other places and keep a copy of the original in your global layout folder and put the localized_date_picker.xml in your region specific folder. Since the system's layout is Month Day Year people in other places may expect that order.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文