当月份选择器值更改时,我的日期选择器值将在使用 xamarin C# 的数字选择器中更改

When month picker value is changed my day picker value will change in number picker using xamarin C#

这是我的代码,我希望当月份值更改时,我的日期选择器值也会更改。日值取决于月份。例如,如果月份值为 1、3、5、7、8、10、12,则日值为 31,否则日值为 30。如果月份为二月,则日值为 28,具体取决于闰年。

我在 C# 中使用 XAMARIN。

谢谢!

public class DayMonthYearPickerDialog : Android.Support.V4.App.DialogFragment
{
    public event EventHandler<DateTime> OnDateTimeChanged;
    public event EventHandler<DateTime> OnClosed;

    public DateTime? Date { get; set; }
    public void Hide() => base.Dialog?.Hide();

    public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
        // Get the layout inflater
        LayoutInflater inflater = Activity.LayoutInflater;
        var selectedDate = GetSelectedDate();

        Calendar cal = Calendar.Instance;

        View dialog = inflater.Inflate(Resource.Layout.date_picker_dialog, null);
        NumberPicker monthPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_month);
        NumberPicker yearPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_year);
        NumberPicker dayPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_day);

        monthPicker.MinValue = 1;
        monthPicker.MaxValue = 12;
        monthPicker.Value = cal.Get(CalendarField.Month) + 1;


        dayPicker.MinValue = 1;
        dayPicker.MaxValue = 31;
        dayPicker.Value = cal.Get(CalendarField.DayOfMonth);

        int year = cal.Get(CalendarField.Year);
        yearPicker.MinValue = 1900;
        yearPicker.MaxValue = year;
        yearPicker.Value = year;

        builder.SetView(dialog)
            .SetPositiveButton("Ok", (sender, e) =>
            {
                selectedDate = new DateTime(yearPicker.Value, monthPicker.Value, dayPicker.Value);

                OnDateTimeChanged?.Invoke(dialog, selectedDate);
            })
            .SetNegativeButton("Cancel", (sender, e) =>
            {
                Dialog.Cancel();
                OnClosed?.Invoke(dialog, selectedDate);
            }); return builder.Create();

您可以将 ValueChanged 事件添加到 monthPicker。如:

 var month = new List<int>() {1,3,5,7,8,10,12 };
           
            monthPicker.ValueChanged += (s, e) =>
            {
                if(month.Contains(monthPicker.Value))
                {
                    dayPicker.MaxValue = 31;
                }else if(monthPicker.Value == 2 && yearPicker.Value %4 == 0)
                {
                    dayPicker.MaxValue = 28;
                }else if(monthPicker.Value == 2 && yearPicker.Value % 4 != 0)
                {
                    dayPicker.MaxValue = 29;
                }
                else
                {
                    dayPicker.MaxValue = 30;
                }
            };

更新 我的意思是您可以使用 DateTime.Now 设置默认值。然后设置当天的最大值 picker.Such 为:

yearPicker.Value = DateTime.Now.Year;
monthPicker.Value = DateTime.Now.Month;
dayPicker.MaxValue = DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month);
dayPicker.Value = DateTime.Now.Day;// Set value must after set the min and max, or the default value will be 0

所以看来我们不需要使用if() else。我们可以只使用以下代码:

monthPicker.ValueChanged += (s, e) =>
{
    dayPicker.MaxValue = DateTime.DaysInMonth(yearPicker.Value,monthPicker.Value);
}