Android:如何将 DatePicker 视图日期格式从 MM/dd/yyyy 更改为 dd/MM/yyyy? [英] Android: How to change the DatePicker view date format from MM/dd/yyyy to dd/MM/yyyy?

查看:31
本文介绍了Android:如何将 DatePicker 视图日期格式从 MM/dd/yyyy 更改为 dd/MM/yyyy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图得到这个问题的答案已经有一段时间了.我还查看了以下链接:Android - DatePicker 小部件格式.在我的手机/模拟器 (v 2.3.3) 上,DatePicker 小部件和对话框都不采用设置中指定的日期格式(设置->日期和时间->选择日期格式).它们始终采用MM/dd/yyyy"格式.语言环境设置为English UK.

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.

当我阅读设置时,我得到d/MM/yyyy":

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

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

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

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

推荐答案

要覆盖 DatePicker 格式,最好的办法是通过对原始代码稍加修改来覆盖其构造函数之一.您可以在代码中为您编辑 ViewGroup,但我会认真考虑不要那样做.

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. 创建类 LocalizedDatePicker
  2. 覆盖 公共 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. 将行 inflater.inflate(R.layout.date_picker, this, true); 替换为 inflater.inflate(my.package.R.layout.localized_date_picker, this, true);
  6. 将原始 date_picker.xml 布局复制到本地资源 localized_date_picker.xml
  7. 编辑所需顺序的文件,因为您正在编辑此文件,请尊重其他地方的本地化,并在全局布局文件夹中保留原始副本,并将 localized_date_picker.xml 放在您的区域特定文件夹中.由于系统的布局是 Month Day Year 其他地方的人可能会期望这个顺序.
  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.

这篇关于Android:如何将 DatePicker 视图日期格式从 MM/dd/yyyy 更改为 dd/MM/yyyy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆