我是Xamarin.android的新手。在我的項目中,我在xamarin.android中使用DatePickerDialog。我想設置一年的選擇限制。我的意思是我必須將年齡限制設定爲21歲。所以用戶不允許從當前日期選擇超過21歲的年齡..如果當前年份是2017年,用戶不應該被允許選擇超過1996年..如何實現這一點..?如果用戶選擇1996年以後的一年,它應該再次回到1996年..在xamarin.android中設置DatePickerDialog中一年的限制選擇
點擊一個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)。但用戶不限於選擇那年以後..如何實現這一個..
你必須設置的21請的年齡限制,明確21歲的人及以上註冊或只與人21可以註冊。 –
是的,我可以做..但我想限制選擇本身.. –
datepicker不應該被允許選擇超過21年.. –