我正在制作一个Dialog。在Initial状态中,DatePickerFragment显示当前日期。然后我将该日期设置为我的TextView。它工作正常。
以下是代码:
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);
}
}
on Activity在我的对话框中使用这种方式。
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;
并创建像这样的点击方法
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
吗?
任何帮助都要得到赞赏。
更新:
添加参数后,它会给出此响应。
答案 0 :(得分:1)
我没有使用DatePickerFragment
,而是按照以下方式使用DatePickerDialog
来解决我的问题。
以这种方式显示DatePicker
ddtime = new DatePickerDialog(context, OnDateSet, DateTime.Today.Year, DateTime.Today.Month - 1,
DateTime.Today.Day
);
ddtime.DatePicker.DateTime = selected_Date;
ddtime.SetTitle("");
ddtime.Show();
它的方法OnDataSet
就像这样
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;
}
最后我的selected_Date
在datePicker中设置如下
ddtime.DatePicker.DateTime = selected_Date;