在 xamarin.android 的 DatePickerDialog 中设置年份的选择限制
Set the limit on selection of a year in DatePickerDialog in xamarin.android
我是 Xamarin.android 的新手。在我的项目中,我在 xamarin.android 中使用 DatePickerDialog。我想将限制设置为年度 selection。我的意思是我必须将 Dob 的年龄限制设置为 21 岁。因此不允许用户 select 从当前日期起超过 21 岁..即,如果当前年份是 2017 年,则不应允许用户 select 超过 1996.. 如何实现这个..?如果用户 select 1996 年之后的那一年应该再次回到 1996..
单击编辑文本时,我将调用 DatePickerFragment class。
所以这是我用过的代码。
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);
}
}
}
点击编辑文本,年份将设置为(当前年份 - 21)。但是用户不限于那一年之后的select..如何实现这一目标..
所以,根据这个 Link ,通过做一些小的改变,日期选择器工作正常。我的意思是现在不允许用户 select 超过 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;
}
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();
我是 Xamarin.android 的新手。在我的项目中,我在 xamarin.android 中使用 DatePickerDialog。我想将限制设置为年度 selection。我的意思是我必须将 Dob 的年龄限制设置为 21 岁。因此不允许用户 select 从当前日期起超过 21 岁..即,如果当前年份是 2017 年,则不应允许用户 select 超过 1996.. 如何实现这个..?如果用户 select 1996 年之后的那一年应该再次回到 1996..
单击编辑文本时,我将调用 DatePickerFragment class。 所以这是我用过的代码。
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);
}
}
}
点击编辑文本,年份将设置为(当前年份 - 21)。但是用户不限于那一年之后的select..如何实现这一目标..
所以,根据这个 Link ,通过做一些小的改变,日期选择器工作正常。我的意思是现在不允许用户 select 超过 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;
}
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();