我正在做一个对话。在初始状态下,datepickerfragment显示当前日期。我把那个日期设置为我的文本视图。很好用。
下面是代码:
DatePickerFragment类

public class DatePickerFragment : DialogFragment,
                                  DatePickerDialog.IOnDateSetListener
    {
        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);
            return dialog;
        }

        public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            DateTime selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth);

            _dateSelectedHandler(selectedDate);

        }
    }

在活动中,在我的对话中使用这种方式。
txt_selectDate = dialog.FindViewById<TextView>(Resource.Id.txt_selectDate);
                        DateTime cuurently = DateTime.Now;
                        txt_selectDate.Text = cuurently.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture);
                        dialog.Show();

                        var select_date = dialog.FindViewById<ImageView>(Resource.Id.select_date);
                        select_date.Click += SelectDate_Click;

并创建这样的click方法
void SelectDate_Click(object sender, EventArgs e)
            {
                DatePickerFragment frag = DatePickerFragment.NewInstance(delegate (DateTime time)
                    {
                        var pickupDate = time.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture);
                        txt_selectDate.Text = time.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

                    });

                frag.Show(context.FragmentManager, DatePickerFragment.TAG);
            }

问题:
现在,当我再次单击以选取新日期时,DatePickerFragment将显示当前日期而不是上一个选定日期。那么,有什么方法可以将上一个日期设置为DatePickerFragment
任何帮助都值得感激。
更新:
android - 有没有办法在Android(Xamarin)中的DatePickerFragment中设置Date-LMLPHP
在添加param之后,它将给出这个响应。

最佳答案

我不使用DatePickerFragment而是使用DatePickerDialog来解决我的问题。
这样显示日期选择器

ddtime = new DatePickerDialog(context, OnDateSet, DateTime.Today.Year, DateTime.Today.Month - 1,
                                                     DateTime.Today.Day
                                                             );

                            ddtime.DatePicker.DateTime = selected_Date;
                            ddtime.SetTitle("");
                            ddtime.Show();

方法是这样的
async void OnDateSet(object sender, DatePickerDialog.DateSetEventArgs e)
            {
                default_date = e.Date.Year + "/" + e.Date.Month + "/" + e.Date.Day;
                txt_selectDate.Text = e.Date.Day + "/" + e.Date.Month + "/" + e.Date.Year;
                selected_Date = e.Date;
            }

最后,我的OnDataSet设置在datepicker中,如下所示
ddtime.DatePicker.DateTime = selected_Date;

10-04 11:57