我正在使用以下代码在xamarin.android上显示dialogefragment,我有2个问题。
我如何在Dialougefragment上禁用过去的日期?
new DatePickerFragment(delegate (DateTime time)
{
var _selectedDate = time;
txtDateTime.Text = _selectedDate.ToString("yyyy.MM.dd");
}) .Show(FragmentManager, DatePickerFragment.TAG);
如何将默认的选择日期设置为当前日期(当前显示在下个月?)>
如果要将默认选择的日期设置为当前日期,则可以设置DatePickerDialog日期时间。
DateTime currently = DateTime.Now; DatePickerDialog dialog = new DatePickerDialog(Activity, this, currently.Year, currently.Month, currently.Day); dialog.DatePicker.DateTime = currently;
如何禁用Dialougefragment上的过去日期?
您可以将今天的日期设置为最短日期。
dialog.DatePicker.MinDate = Java.Lang.JavaSystem.CurrentTimeMillis();
这里是DatePickerFragment.cs:
public class DatePickerFragment : DialogFragment, DatePickerDialog.IOnDateSetListener { // TAG can be any string that you desire. 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, currently.Day); dialog.DatePicker.DateTime = currently; dialog.DatePicker.MinDate = Java.Lang.JavaSystem.CurrentTimeMillis(); return dialog; } }
MainActivity.cs:
public class MainActivity : Activity { TextView _dateDisplay; Button _dateSelectButton; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); _dateDisplay = FindViewById<TextView>(Resource.Id.date_display); _dateSelectButton = FindViewById<Button>(Resource.Id.date_select_button); _dateSelectButton.Click += DateSelect_OnClick; } void DateSelect_OnClick(object sender, EventArgs eventArgs) { DatePickerFragment frag = DatePickerFragment.NewInstance(delegate(DateTime time) { _dateDisplay.Text = time.ToLongDateString(); }); frag.Show(FragmentManager, DatePickerFragment.TAG); } }
截图:
如果我的回复对您有帮助,请记住将我的回复标记为答案,谢谢。