android - 最小日期最大日期在 xamarin android 中不起作用

标签 android xamarin.android android-datepicker datepickerdialog

我正在使用 xamarin android datepicker 对话框 fragment 。但试图启用今天和 toady + 3 天之间的日期。它不工作。它甚至不适用于最小日期选项。

public static readonly string TAG = "X:" + typeof (DatePickerFragment).Name.ToUpper();
        Action<DateTime> _dateSelectedHandler = delegate { };

        public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            // Note: monthOfYear is a value between 0 and 11, not 1 and 12!
            DateTime selectedDate = new DateTime(year, monthOfYear +1, dayOfMonth);
            Log.Debug(TAG, selectedDate.ToLongDateString());
            _dateSelectedHandler(selectedDate);
        }

        public static DatePickerFragment NewInstance(Action<DateTime> onDateSelected)
        {
            DatePickerFragment frag = new DatePickerFragment();
            frag._dateSelectedHandler = onDateSelected;
            return frag;
        }

        public override Dialog OnCreateDialog(Bundle savedInstanceState)
        {
            DateTime currently = DateTime.Now;
            DatePickerDialog dialog = new DatePickerDialog(Activity, this, currently.Year, currently.Month - 1,
                                                           currently.Day);

            dialog.DatePicker.MinDate = currently.Millisecond;
            dialog.DatePicker.MinDate = currently.AddDays(3).Millisecond;

            return dialog;
        }

最佳答案

您可以更改 OnCreateDialog 的代码。

 public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        DateTime currently = DateTime.Now;
        DatePickerDialog dialog = new DatePickerDialog(Activity,
                                                       this,
                                                       currently.Year,
                                                       currently.Month - 1,
                                                       currently.Day);

       dialog.DatePicker.MinDate = (long)(DateTime.Now.Date - new DateTime(1970, 1, 1)).TotalMilliseconds-1000 * 60 * 60 * 24 * 3;
       dialog.DatePicker.MaxDate = (long)(DateTime.Now.Date - new DateTime(1970, 1, 1)).TotalMilliseconds + 1000 * 60 * 60 * 24 * 3;
        return dialog;
    }

有DatePicker的截图。注意:我设置的最小日期是三天前,最大日期是三天后。

enter image description here

关于android - 最小日期最大日期在 xamarin android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54353741/

相关文章:

Java for 循环永不终止

android - EditText 的提示被切断,因为我的提示文本超过一行。 editText 只显示一行。我怎样才能使整个提示可见?

android - 如何显示最新的DatePickerDialog和TimePickerDialog android

android - 隐藏在软键盘后面的日期选择器对话框

c# - 如何在 Xamarin.Forms 中处理 WCF Duplex?

android - 如何从当前日期设置日期 1 年?

java - 如何在 Android 中添加卡片 View 列表,并且只能通过滑动删除其中一个卡片 View ?

javascript - 如何从代码中更改 jquery 移动翻转开关状态

c# - Android 语音识别将数据传回 Xamarin Forms

c# - MvxAppCompatDialogFragment 类中缺少 EnsureBindingContextSet 方法