如何在Xamarin.Forms Android微调器中更改日期顺序(从默认更改为日/月/年)?

时间:2019-10-07 15:37:22

标签: android xamarin.forms android-spinner

我已经用微调器替换了数据选择器的默认样式,但是现在我需要将(微调器而不是“结果”的)日期格式更改为在所有区域性中都相同(dd / MM / yyyy) )。

我试图更改格式,但这是占位符的格式...

我确定可以在styles.xml中进行更改,但是我不知道该怎么做。

在styles.xml中,我有:

<item name="android:dialogTheme">@style/MyDialogTheme</item>
<item name="android:datePickerStyle">@style/MyDatePicker</item>

<style name="MyDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:datePickerStyle">@style/MyDatePicker</item>
<item name="android:datePickerMode">spinner</item>
<item name="android:calendarViewShown">false</item>
<item name="android:spinnersShown">true</item>
</style>

<style name="MyDatePicker" parent="android:Widget.Material.Light.DatePicker">
<item name="android:datePickerMode">spinner</item>
<item name="android:calendarViewShown">false</item>
<item name="android:spinnersShown">true</item>
</style>

有一种方法可以在此处或在自定义渲染器中进行更改?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我发现此answer很有用,但它适用于本机android,我必须对Xamarin.Forms Android进行一些更改,创建自定义渲染器并添加以下行:

protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
{
    var dialog = base.CreateDatePickerDialog(year, month, day);
    dialog.Show();
    OrderDate(dialog, new char[] { 'd', 'm', 'y' });
    return dialog;
}

private void OrderDate(DatePickerDialog dialog, char[] ymdOrder)
{
    if (!dialog.IsShowing)
    {
        return;
    }


     int idYear = Resources.GetIdentifier("year", "id", "android");
     int idMonth = Resources.GetIdentifier("month", "id", "android");
     int idDay = Resources.GetIdentifier("day", "id", "android");
     int idLayout = Resources.GetIdentifier("pickers", "id", "android");

     NumberPicker spinnerYear = (NumberPicker)dialog.FindViewById(idYear);
     NumberPicker spinnerMonth = (NumberPicker)dialog.FindViewById(idMonth);
     NumberPicker spinnerDay = (NumberPicker)dialog.FindViewById(idDay);
     LinearLayout layout = (LinearLayout)dialog.FindViewById(idLayout);

    layout.RemoveAllViews();
    for (int i = 0; i < SPINNER_COUNT; i++)
    {
        switch (ymdOrder[i])
        {
            case 'y':
                layout.AddView(spinnerYear);
                setImeOptions(spinnerYear, i);
                break;
            case 'm':
                layout.AddView(spinnerMonth);
                setImeOptions(spinnerMonth, i);
                break;
            case 'd':
                layout.AddView(spinnerDay);
                setImeOptions(spinnerDay, i);
                break;
        }
    }
}

private void setImeOptions(NumberPicker spinner, int spinnerIndex)
{
    ImeAction imeOptions;
    if (spinnerIndex < SPINNER_COUNT - 1)
    {
        imeOptions = inputMethos.ImeAction.Next;
    }
    else
    {
        imeOptions = inputMethos.ImeAction.Done;
    }
    int idPickerInput = Resources.GetIdentifier("numberpicker_input", "id", "android");
    TextView input = (TextView)spinner.FindViewById(idPickerInput);
    input.SetImeActionLabel("",imeOptions);
}

答案 1 :(得分:0)

Android上的DatePicker(微调器)默认为用户手机上的所选设置。通常建议不要覆盖本机行为。

不过,您可以按照以下显示的步骤,通过创建自定义渲染器以使用自定义DatePicker来覆盖此行为:https://stackoverflow.com/a/7208968/11104068