如何禁用日期选择器Dialouge片段上的最小日期

问题描述 投票:0回答:1

我正在使用以下代码在xamarin.android上显示dialogefragment,我有2个问题。

  1. 我如何将默认的选择日期设置为当前日期(当前是显示下个月)?
  2. 我如何在Dialougefragment上禁用过去的日期?

    new DatePickerFragment(delegate (DateTime time)
            {
    
            var _selectedDate = time;
            txtDateTime.Text = _selectedDate.ToString("yyyy.MM.dd");
    
        }) .Show(FragmentManager, DatePickerFragment.TAG);
    
c# android xamarin xamarin.android datepicker
1个回答
0
投票

如何将默认的选择日期设置为当前日期(当前显示在下个月?)>

如果要将默认选择的日期设置为当前日期,则可以设置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);
    }
}

截图:

enter image description here

如果我的回复对您有帮助,请记住将我的回复标记为答案,谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.