2017-04-19 65 views
0

我是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)。但用戶不限於選擇那年以後..如何實現這一個..

+0

你必須設置的21請的年齡限制,明確21歲的人及以上註冊或只與人21可以註冊。 –

+0

是的,我可以做..但我想限制選擇本身.. –

+0

datepicker不應該被允許選擇超過21年.. –

回答

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(); 
+0

它運作良好..但有一個改變..'DatePickerDialog對話框=新的DatePickerDialog(this.Activity,this,目前。年 - 21,目前。月 - 1,目前.Day);'..謝謝你..! ! –

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; 
     } 
相關問題