0

我声明了以下变量:

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.03.2018当它必须是今天的日期时被选中09.02.2018。然后,当我从显示的日历中选择某个日期时,它会从我选择的日期中选择一个向后一个月的日期,例如,如果我选择10.3.2018它说我已经选择了10.02.2018 这是我的所有活动:

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();
        };

}
4

1 回答 1

1

当它必须是今天的日期 09.02.2018 时,它会显示一个选择 09.03.2018 的日历

请检查您手机的时间,它在我的手机上有效。

如果我选择 10.3.2018 它说我选择了 10.02.2018

是的,在该OnDateSet方法中,月份从 0 开始,0 表示 1 月,1 表示 2 月。您需要使用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();
        };
    }
}
于 2018-02-12T02:39:35.857 回答