DatePickerDialog 显示的月份比其构造函数中设置的提前(Xamarin.Android)

DatePickerDialog showing with month forward than what is set in its constructor(Xamarin.Android)

我声明了以下变量:

private int year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day;

我创建 DatePickerDialog:

DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);

当我调用它的方法时显示:

datePickerDialog.Show();

当日期必须是今天 09.02.2018 时,它会显示一个选择了 09.03.2018 的日历。然后,当我从显示的日历中选择某个日期时,它会选择一个从我选择的日期向后一个月的日期,例如,如果我选择 10.3.2018 它说我选择了 10.02.2018 这是我所有的 activity:

public class OrdersActivity : Activity, IOnDateSetListener
{
    private int year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day;
    Button btnDatePicker;

    //set btnDatePicker's text to be whatever user has selected
    public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
    {
        this.year = year;
        this.month = month;
        this.day = dayOfMonth;
        string selectedDate = String.Format(day + "." + month + "." + year);
        btnDatePicker.Text = selectedDate;
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Orders);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);

        //set btnDatePicker's text to be DateTime.Now
        btnDatePicker = FindViewById<Button>(Resource.Id.btnDatePicker);
        btnDatePicker.Text = DateTime.Now.ToString("dd.MM.yyyy");

        btnDatePicker.Click += delegate {
            datePickerDialog.Show();
        };

}

It displays a calendar with 09.03.2018 selected when it has to be the date today 09.02.2018

请检查您的 phone 时间,它适用于我的 phone。

if I choose 10.3.2018 It says me that I've selected 10.02.2018

是的,在 OnDateSet 方法中,月份从 0 开始,0 表示一月,1 表示二月。您需要使用 this.month=month+1;,并替换

string selectedDate = String.Format(day + "." + month + "." + year);

string selectedDate = String.Format(day + "." + this.month + "." + year);


更新:

正如我所说,月份是从0开始的,所以你需要-1+1,根据你的代码看下面的代码。

public class MainActivity : Activity, IOnDateSetListener
{
    private int year = DateTime.Now.Year, month = DateTime.Now.Month-1, day = DateTime.Now.Day;
    Button btnDatePicker;

    //set btnDatePicker's text to be whatever user has selected 
    public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
    {
        this.year = year;
        this.month = month+1;
        this.day = dayOfMonth;
        string selectedDate = String.Format(day + "." + this.month + "." + year);
        btnDatePicker.Text = selectedDate;
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Main);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);

        //set btnDatePicker's text to be DateTime.Now 
        btnDatePicker = FindViewById<Button>(Resource.Id.btnDatePicker);
        btnDatePicker.Text = DateTime.Now.ToString("dd.MM.yyyy");

        btnDatePicker.Click += delegate
        {
            datePickerDialog.Show();
        };
    }
}