Issue
This is my code, I would like when month value is changed my day picker value will change. Day value is depends on month. Like, if month value is 1,3,5,7,8,10,12 then day value will 31 otherwise day value 30. If month is feb then day value will 28 and will depends on leap years.
I'm using XAMARIN with C#.
Thank you!
public class DayMonthYearPickerDialog : Android.Support.V4.App.DialogFragment
{
public event EventHandler<DateTime> OnDateTimeChanged;
public event EventHandler<DateTime> OnClosed;
public DateTime? Date { get; set; }
public void Hide() => base.Dialog?.Hide();
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
// Get the layout inflater
LayoutInflater inflater = Activity.LayoutInflater;
var selectedDate = GetSelectedDate();
Calendar cal = Calendar.Instance;
View dialog = inflater.Inflate(Resource.Layout.date_picker_dialog, null);
NumberPicker monthPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_month);
NumberPicker yearPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_year);
NumberPicker dayPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_day);
monthPicker.MinValue = 1;
monthPicker.MaxValue = 12;
monthPicker.Value = cal.Get(CalendarField.Month) + 1;
dayPicker.MinValue = 1;
dayPicker.MaxValue = 31;
dayPicker.Value = cal.Get(CalendarField.DayOfMonth);
int year = cal.Get(CalendarField.Year);
yearPicker.MinValue = 1900;
yearPicker.MaxValue = year;
yearPicker.Value = year;
builder.SetView(dialog)
.SetPositiveButton("Ok", (sender, e) =>
{
selectedDate = new DateTime(yearPicker.Value, monthPicker.Value, dayPicker.Value);
OnDateTimeChanged?.Invoke(dialog, selectedDate);
})
.SetNegativeButton("Cancel", (sender, e) =>
{
Dialog.Cancel();
OnClosed?.Invoke(dialog, selectedDate);
}); return builder.Create();
Solution
You can add a ValueChanged event to the monthPicker. Such as:
var month = new List<int>() {1,3,5,7,8,10,12 };
monthPicker.ValueChanged += (s, e) =>
{
if(month.Contains(monthPicker.Value))
{
dayPicker.MaxValue = 31;
}else if(monthPicker.Value == 2 && yearPicker.Value %4 == 0)
{
dayPicker.MaxValue = 28;
}else if(monthPicker.Value == 2 && yearPicker.Value % 4 != 0)
{
dayPicker.MaxValue = 29;
}
else
{
dayPicker.MaxValue = 30;
}
};
Update
What I mean is that you can set the default value with the DateTime.Now
. And then set the maxvalue of the day picker.Such as:
yearPicker.Value = DateTime.Now.Year;
monthPicker.Value = DateTime.Now.Month;
dayPicker.MaxValue = DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month);
dayPicker.Value = DateTime.Now.Day;// Set value must after set the min and max, or the default value will be 0
So it seems that we needn't to use if() else
. We can just use the following code:
monthPicker.ValueChanged += (s, e) =>
{
dayPicker.MaxValue = DateTime.DaysInMonth(yearPicker.Value,monthPicker.Value);
}
Answered By - Liyun Zhang - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.