Set default Locale to DatePicker and TimePicker

两盒软妹~` 提交于 2019-12-31 01:19:06

问题


I'm using a DatePicker and a TimePicker in a custom Dialog. The functionality works fine, but I have a problem when I change the default Locale of the app to match the Arabic language.

The problem is that the DatePicker and TimePicker are not localized on all Android versions. It works on Android versions 4.2.2 and higher. But It doesn't work on Android versions 4.1.2 And lower.

I want to either change localization of DatePicker and TimePicker to English all the time, Or to change it to Arabic when the Locale matches the Arabic language.

This is the dialog in Android versions 4.2.2 and higher (the correct one that matches the localization)

And this is the dialog in Android versions 4.1.2 and lower


回答1:


You need to create your custom DatePicker or TimePicker . See this link https://stackoverflow.com/a/9220899/3864698. It can help you to solve your problem.

Or you can use Reflection to edit the NumberPicker values, follow this link for the complete solution.




回答2:


Please change the locale in your fragment/activity, before showing DatePicker/TimePicker.

Locale locale = Locale.US; //Your specific locale. Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    configuration.setLayoutDirection(locale);

    context.createConfigurationContext(configuration);


来源:https://stackoverflow.com/questions/26487718/set-default-locale-to-datepicker-and-timepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!