我正在做一个对话。在初始状态下,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
?任何帮助都值得感激。
更新:
在添加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;