在android DatePickerDialog中将语言设置为法语
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21257014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set language to French in android DatePickerDialog
提问by begiPass
Is there any way to have date displayed in DatePickerDialog
in french
有没有办法DatePickerDialog
用法语显示日期
I have searched about this but found no results
我已经搜索过这个,但没有找到结果
Here is my code:
这是我的代码:
Calendar c = Calendar.getInstance();
picker = new DatePickerDialog(PaymentView.this,
PaymentView.this,
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
picker.setIcon(R.drawable.ic_launcher);
picker.setTitle("Choisir la date");
picker.getDatePicker().setMinDate(System.currentTimeMillis() - 2000);
Instead of Fri, Nov 21, 2014 I want to have french abbreviation I have also added that before instantiate it :
而不是 2014 年 11 月 21 日星期五,我想使用法语缩写,我还在实例化它之前添加了它:
Locale locale = new Locale("FR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, null);
回答by lightbyte
I made it!
我做到了!
I have added these 2 lines before all the DatePickerDialog stuff:
我在所有 DatePickerDialog 内容之前添加了这两行:
Locale locale = getResources().getConfiguration().locale;
Locale.setDefault(locale);
and after that:
在那之后:
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,
this, mYear, mMonth, mDay);
dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
dialog.setTitle(R.string.main_first_day_of_your_last_period);
dialog.show();
I hope it helps someone.
我希望它可以帮助某人。
回答by pierre g
You can just add this declaration before DatePicker
:
您可以在之前添加此声明DatePicker
:
Locale.setDefault(Locale.FRANCE);
回答by Igor K
I had almost similar problem and I found solution for this answer here
我遇到了几乎类似的问题,我在这里找到了这个答案的解决方案
In my case I needed:
就我而言,我需要:
have
initPicker(context)
method from the link/** * Use reflection to use the Locale of the application for the month spinner. * * PS: DAMN DATEPICKER DOESN'T HONOR Locale.getDefault() * <a href="http://code.google.com/p/android/issues/detail?id=25107">Bug Report</a> * @param context */ public void initPicker(Context context) { String monthPickerVarName; String months[] = context.getResources().getStringArray(R.array.short_months); if (Build.VERSION.SDK_INT >= 14) { monthPickerVarName = "mMonthSpinner"; } else { monthPickerVarName = "mMonthPicker"; } try { Field f[] = mDatePicker.getClass().getDeclaredFields(); for (Field field : f) { if (field.getName().equals("mShortMonths")) { field.setAccessible(true); field.set(mDatePicker, months); } else if (field.getName().equals(monthPickerVarName)) { field.setAccessible(true); Object o = field.get(mDatePicker); if (Build.VERSION.SDK_INT >= 14) { Method m = o.getClass().getDeclaredMethod("setDisplayedValues", String[].class); m.setAccessible(true); m.invoke(o, (Object)months); } else { Method m = o.getClass().getDeclaredMethod("setRange", int.class, int.class, String[].class); m.setAccessible(true); m.invoke(o, 1, 12, (Object)months); } } } } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } try { final Method updateSpinner = mDatePicker.getClass().getDeclaredMethod("updateSpinners"); updateSpinner.setAccessible(true); updateSpinner.invoke(mDatePicker); updateSpinner.setAccessible(false); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } }
to call
initPicker(context)
before call topicker.init(...)
inonViewCreated
method of my custom widgetDefine short_months in res/values/arrays.xml
<string-array name="months_values"> <item>Jan</item> <item>Feb</item> <item>Mar</item> <item>Apr</item> <item>May</item> <item>Jun</item> <item>Jul</item> <item>Aug</item> <item>Sep</item> <item>Oct</item> <item>Nov</item> <item>Dec</item> </string-array>
Define short_months in res/values-ru/arrays.xml (in this question must be analogue res/values-fr/arrays.xml)
<string-array name="months_values"> <item>Янв</item> <item>Фев</item> <item>Мар</item> <item>Апр</item> <item>Май</item> <item>Июн</item> <item>Июл</item> <item>Авг</item> <item>Сеп</item> <item>Окт</item> <item>Ноя</item> <item>Дек</item> </string-array>
有
initPicker(context)
来自链接的方法/** * Use reflection to use the Locale of the application for the month spinner. * * PS: DAMN DATEPICKER DOESN'T HONOR Locale.getDefault() * <a href="http://code.google.com/p/android/issues/detail?id=25107">Bug Report</a> * @param context */ public void initPicker(Context context) { String monthPickerVarName; String months[] = context.getResources().getStringArray(R.array.short_months); if (Build.VERSION.SDK_INT >= 14) { monthPickerVarName = "mMonthSpinner"; } else { monthPickerVarName = "mMonthPicker"; } try { Field f[] = mDatePicker.getClass().getDeclaredFields(); for (Field field : f) { if (field.getName().equals("mShortMonths")) { field.setAccessible(true); field.set(mDatePicker, months); } else if (field.getName().equals(monthPickerVarName)) { field.setAccessible(true); Object o = field.get(mDatePicker); if (Build.VERSION.SDK_INT >= 14) { Method m = o.getClass().getDeclaredMethod("setDisplayedValues", String[].class); m.setAccessible(true); m.invoke(o, (Object)months); } else { Method m = o.getClass().getDeclaredMethod("setRange", int.class, int.class, String[].class); m.setAccessible(true); m.invoke(o, 1, 12, (Object)months); } } } } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } try { final Method updateSpinner = mDatePicker.getClass().getDeclaredMethod("updateSpinners"); updateSpinner.setAccessible(true); updateSpinner.invoke(mDatePicker); updateSpinner.setAccessible(false); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } }
拨打
initPicker(context)
电话之前,picker.init(...)
在onViewCreated
我的自定义窗口小部件的方法在 res/values/arrays.xml 中定义 short_months
<string-array name="months_values"> <item>Jan</item> <item>Feb</item> <item>Mar</item> <item>Apr</item> <item>May</item> <item>Jun</item> <item>Jul</item> <item>Aug</item> <item>Sep</item> <item>Oct</item> <item>Nov</item> <item>Dec</item> </string-array>
在 res/values-ru/arrays.xml 中定义 short_months(在这个问题中必须是模拟 res/values-fr/arrays.xml)
<string-array name="months_values"> <item>Янв</item> <item>Фев</item> <item>Мар</item> <item>Апр</item> <item>Май</item> <item>Июн</item> <item>Июл</item> <item>Авг</item> <item>Сеп</item> <item>Окт</item> <item>Ноя</item> <item>Дек</item> </string-array>
回答by Tobrun
Relevant classes:
相关类:
http://developer.android.com/reference/android/app/DatePickerDialog.htmlhttp://developer.android.com/reference/android/widget/DatePicker.htmlhttp://developer.android.com/reference/android/widget/CalendarView.html
http://developer.android.com/reference/android/app/DatePickerDialog.html http://developer.android.com/reference/android/widget/DatePicker.html http://developer.android.com/reference/ android/widget/CalendarView.html
Currently I don't see any possibility to use the standard one with usage of localisation options or reuse the timepickerdialog and datepicker implementation to implement your own calenderview (you can get the calenderview but you can't set a calenderview). You can always implement your own custom dialog.
目前,我看不到使用标准选项和本地化选项或重用 timepickerdialog 和 datepicker 实现来实现您自己的日历视图的任何可能性(您可以获得日历视图,但不能设置日历视图)。您始终可以实现自己的自定义对话框。
Also I found this opensource library on the web.
我也在网上找到了这个开源库。
It indicates that it would handle local settings. Haven't tried it though.
它表明它将处理本地设置。不过没试过。
回答by Nikolai
Igor's answer helped me a lot. But I'd like to add that you might run into NoSuchMethodException
as DoubleK pointed out, because in some TimePicker
and DatePicker
implementations there're separate classes like TimePickerSpinnerDelegate
and DatePicker.DatePickerSpinnerDelegate
which contain these variables and methods. Here's how I updated the pickers (for API 14+):
伊戈尔的回答对我帮助很大。但我想补充一点,你可能会遇到NoSuchMethodException
如DoubleK指出,因为在一些TimePicker
和DatePicker
实现是有单独的类象TimePickerSpinnerDelegate
和DatePicker.DatePickerSpinnerDelegate
含有这些变量和方法。以下是我更新选择器的方式(适用于 API 14+):
private void initPicker(Object object, String[] values) {
try {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
// If there's a delegate, we use it instead.
if (field.getName().equals("mDelegate")) {
field.setAccessible(true);
object = field.get(object);
fields = object.getClass().getDeclaredFields();
break;
}
}
for (Field field : fields) {
if (field.getName().equals("mAmPmStrings") ||
field.getName().equals("mShortMonths")) {
field.setAccessible(true);
field.set(object, values);
} else if (field.getName().equals("mAmPmSpinner") ||
field.getName().equals("mMonthSpinner")) {
field.setAccessible(true);
Object innerObject = field.get(object);
Method method = innerObject.getClass().getDeclaredMethod(
"setDisplayedValues", String[].class);
method.setAccessible(true);
method.invoke(innerObject, (Object) values);
}
}
Method[] methods = object.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("updateAmPmControl") ||
method.getName().equals("updateSpinners")) {
method.setAccessible(true);
method.invoke(object);
break;
}
}
} catch (Exception e) {
Log.e(APP_TAG, e.getMessage(), e);
}
}
So I just call
所以我只是打电话
initPicker(timePicker, resources.getStringArray(R.array.am_pm));
and
和
initPicker(datePicker, resources.getStringArray(R.array.calendar_months));
after the views are created and everything works as expected.
创建视图后,一切都按预期工作。
回答by Bajrang Hudda
You may try changing context of DateDialogPicker dialog. Whenever such problem occurs most of the time it's because of your current context.
您可以尝试更改 DateDialogPicker 对话框的上下文。每当出现此类问题时,大部分时间都是因为您当前的上下文。
I had same problem, sometimes I was able to change the languagle but some times not, so I tried changing context and now it's working fine. Before it was fragment context (requireContext()) as below -
我遇到了同样的问题,有时我可以更改语言,但有时不能,所以我尝试更改上下文,现在它工作正常。在它是片段上下文(requireContext())之前,如下所示 -
datePickerDialog = DatePickerDialog(
requireContext(), R.style.TimePickerTheme.....
but later I changed to below (activity context - requireActivity()) that is working fine-
但后来我改为下面(活动上下文 - requireActivity()),它工作正常 -
datePickerDialog = DatePickerDialog(
requireActivity(), R.style.TimePickerTheme.....