设置 Android DatePicker 标题语言 [英] Set Android DatePicker title language

查看:24
本文介绍了设置 Android DatePicker 标题语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个支持 4 种语言的应用,可以在应用中选择.在 Android 上,DatePicker 有一个标题.这个标题,即使在设置了语言环境之后,似乎总是有利于设置语言环境的设备.只有标题会这样做,因为 DatePicker 的功能部分使用应用程序选择的语言.

I have an app that is available in 4 languages, which can be chosen within the app. On Android, the DatePicker has a title. This title, even after setting the locale, always seems to favor the devices set locale. Only the title does this, as the functional part of the DatePicker is in the app-chosen language.

这是应用设置为韩语时的样子.

Here is what it looks like when the app is set to Korean.

如何将 8 月 24 日星期四更改为韩语?我已将 Xamarin 和 Android 语言环境都设置为韩语.我可以设置日期选择器渲染器中的属性吗?

How do I go about Changing that Thu, Aug 24 to Korean? I've set both Xamarin and Android locale to Korean. Is there an attribute in the Date Picker renderer I can set?

谢谢.

推荐答案

如果您正在执行这样的操作来应用新的语言环境上下文基础配置:

If you are doing something like this to apply a new locale context base configuration:

protected override void AttachBaseContext(Android.Content.Context @base)
{
    Locale locale = Locale.Korean;
    Locale.SetDefault(Locale.Category.Format, locale);
    @base.Resources.Configuration.SetLocale(locale);
    var newContext = @base.CreateConfigurationContext(@base.Resources.Configuration);
    base.AttachBaseContext(newContext);
}

Material-design CalendarView 没有从传递给它的 .ctor 的上下文中正确地尊重上下文的区域设置,并且您最终得到了错误的标题本地化,如您的问题所示;-(

The Material-design CalendarView does not honor the context's locale correctly from the one that is passed to its .ctor and you end up with the wrong Title localization as shown in your question ;-(

一种选择是子类化 DatePicker 并重新实现 DatePickerCalendarDelegate 并将其应用于子类化的 AlertDialog,但这有点为正确解决问题而进行的大量编码.

One option is to subclass DatePicker and re-implement DatePickerCalendarDelegate and apply that to a sub-classed AlertDialog, but that is a bit crazy amount of coding to correctly address the issue.

所以这是我一直在使用的修复(hack)(针对 SO 进行了简化,因此您需要对各种 API 级别检查等进行 API):

So this is a fix (hack) that I have been using (simplified for SO, so you will need to API the various API level checks, etc..):

// globals/cached
bool headerChangeFlag = true;
TextView headerTextView;
string headerDatePatternLocale;
SimpleDateFormat monthDayFormatLocale;

void SetHeaderMonthDay(DatePickerDialog dialog, Locale locale)
{
    if (headerTextView == null)
    {
        // Material Design formatted CalendarView being used, need to do API level check and skip on older APIs
        var id = base.Resources.GetIdentifier("date_picker_header_date", "id", "android");
        headerTextView = dialog.DatePicker.FindViewById<TextView>(id);
        headerDatePatternLocale = Android.Text.Format.DateFormat.GetBestDateTimePattern(locale, "EMMMd");
        monthDayFormatLocale = new SimpleDateFormat(headerDatePatternLocale, locale);
        headerTextView.SetTextColor(Android.Graphics.Color.Red);
        headerTextView.TextChanged += (sender, e) =>
        {
            headerChangeFlag = !headerChangeFlag;
            if (!headerChangeFlag)
                return;
            SetHeaderMonthDay(dialog, locale);
        };
    }
    var selectedDateLocale = monthDayFormatLocale.Format(new Date((long)dialog.DatePicker.DateTime.ToUniversalTime().Subtract(
              new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc)).TotalMilliseconds));
    headerTextView.Text = selectedDateLocale;
}

用法:

var dialog = new DatePickerDialog(this);
dialog.Show();
SetHeaderMonthDay(dialog, Locale.Korean); // Call once, the text change event will update it when user changes date...

结果:

这篇关于设置 Android DatePicker 标题语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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