简体   繁体   English

Android DatePickerDialog年

[英]Android DatePickerDialog Year

I have a question for you that will be easy. 我有一个问题很容易解决。 I have a DatePickerDialog to select the day, month and year. 我有一个DatePickerDialog来选择日期,月份和年份。 Now I would like to receive the default next year, not the current one. 现在,我想收到明年的默认值,而不是当前的默认值。 I tried to insert + 1 but it is not the desired result. 我试图插入+ 1,但这不是理想的结果。 thanks 谢谢

public void chooseDate2(View v) {
    new DatePickerDialog(Inserisci_en_us.this, d2,
                          dateAndTime.get(Calendar.YEAR),
                         dateAndTime.get(Calendar.MONTH),
                          dateAndTime.get(Calendar.DAY_OF_MONTH))
      .show();
  }
  private void updateLabel2() {
      scadenza.setText(fmtDateAndTime.format(dateAndTime.getTime()));           
  }
  DatePickerDialog.OnDateSetListener d2=new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        dateAndTime.set(Calendar.YEAR, year);
      dateAndTime.set(Calendar.MONTH, monthOfYear);
      dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
      updateLabel2();
    }
  };

I guess you want the next year as a default year when you open your date picker, right? 我猜您想在打开日期选择器时将明年作为默认年份,对吗? I know you mentioned adding 1, but I guess you did it wrong. 我知道您提到加1,但我想您做错了。 Just do the following: 只需执行以下操作:

Calenar c = Calendar.getInstance();
new DatePickerDialog(Inserisci_entrate_uscite.this, d2,
                      c.get(Calendar.YEAR) + 1,
                     dateAndTime.get(Calendar.MONTH),
                      dateAndTime.get(Calendar.DAY_OF_MONTH))
  .show();

This should return the next year even if many dialogs are opened. 即使打开许多对话框,也应该在明年返回。 (If the dateAndTime is the current date). (如果dateAndTime是当前日期)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM