簡體   English   中英

如何在DatePickerDialog中設置最小日期而不是今天的特定日期(XAMARIN)

[英]How to set min date in DatePickerDialog with a specific date not today (XAMARIN)

我想要的是將要禁用的特定日期之前的日期(不是今天之前)設置為。 例如:

今天是2017年5月5日,目標特定日期:僅5月1日-5月5日。

使用此代碼將禁用所有大於這一天的代碼

        dialog.DatePicker.MaxDate = Java.Lang.JavaSystem.CurrentTimeMillis();

但是我不能從5月1日之前禁用它。

我現在有這段代碼。

public class DatePickerFragment : DialogFragment,
                              DatePickerDialog.IOnDateSetListener
{
    // TAG can be any string of your choice.
    public static readonly string TAG = "X:" + typeof(DatePickerFragment).Name.ToUpper();

    // Initialize this value to prevent NullReferenceExceptions.
    Action<DateTime> _dateSelectedHandler = delegate { };

    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);

        //****************this is my problem*****************//
        dialog.DatePicker.MinDate = CurrentUser.lastReplenish.Millisecond;
        dialog.DatePicker.MaxDate = Java.Lang.JavaSystem.CurrentTimeMillis();
        //***************************************************//
        return dialog;
    }

    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);
    }
}

一世

我猜“ CurrentUser.lastReplenish”是一個.NET DateTime對象? Android始終需要從1970年1月1日(紀元)開始的毫秒數,因此您需要計算一下:

dialog.DatePicker.MinDate = (long)CurrentUser.lastReplenish.ToUniversalTime()
    .Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM