在xamarin.android中的DatePickerDialog中设置一年选择的限制

时间:2017-04-19 06:52:17

标签: android xamarin.android datepickerdialog

我是Xamarin.android的新手。在我的项目中,我在xamarin.android中使用DatePickerDialog。我想设置年度选择的限制。我的意思是我必须将年龄限制设为21岁。因此,不允许用户从当前日期选择年龄超过21岁的年龄..如果当前年份是2017年,则不应允许用户选择超过1996年......如何实现这一个...?如果用户选择1996年以后的那一年它应该再次回到1996年。

点击edittext后,我正在调用DatePickerFragment类。 所以这是我用过的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace MobileApplication.Droid
{
    public class DatePickerFragment : DialogFragment,
                                  DatePickerDialog.IOnDateSetListener
    {
        // TAG can be any string of your choice.
        public static readonly string TAG = "X:" + typeof(DatePickerFragment).Name.ToUpper();

        // Initialize this value to prevent NullReferenceExceptions.
        Action<string> _dateSelectedHandler = delegate { };

        public static DatePickerFragment NewInstance(Action<string> 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 - 21,
                                                           currently.Month,
                                                           currently.Day);
             dialog.DatePicker.MaxDateTime.DayOfYear.Equals(currently.Year - 21);
            return dialog;
        }

        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);
            string selectedDate = (monthOfYear + 1).ToString().PadLeft(2, '0') + "/" + (dayOfMonth).ToString().PadLeft(2, '0') + "/" + year.ToString();

            //Log.Debug(TAG, selectedDate.ToLongDateString());
            Log.Debug(TAG, selectedDate);
            _dateSelectedHandler(selectedDate);
        }
    }
}

点击edittext,年份将设置为(当前年份 - 21)。但是用户不限制在那一年之后选择..如何实现这一目标..

2 个答案:

答案 0 :(得分:1)

DateTime currently = DateTime.Now;
            DatePickerDialog dialog = new DatePickerDialog(this, delegate { }, currently.Year -21, currently.Month-1, currently.Day);

            dialog.DatePicker.MaxDate = (long)DateTime.Now.AddYears(-21)
                .ToUniversalTime()
                .Subtract(DateTime.MinValue.AddYears(1969))
                .TotalMilliseconds;
            dialog.Show();

答案 1 :(得分:0)

所以,根据这个 Link ,通过做一些小的更改,日期选择器工作正常。我的意思是现在不允许用户选择超过21岁。

所以这些是我以前的代码所做的更改..

public override Dialog OnCreateDialog(Bundle savedInstanceState)
        {
            DateTime currently = DateTime.Now;
            DatePickerDialog dialog = new DatePickerDialog(Activity,
                                                           this,
                                                           currently.Year - 21,
                                                           currently.Month,
                                                           currently.Day);

            dialog.DatePicker.MaxDate = new Java.Util.Date().Time - 662300000000; // 662300000000 => 1 year is converted to milliseconds..
            return dialog;
        }