如何将datepicker值设置为选定日期android

时间:2016-02-23 23:55:39

标签: android datepicker

我有值Birthdate但我想在datepicker对话框中显示/选择生日。例如1997年3月1日 - >在datepicker对话框中选择此日期。我试过但只是错误。我无法找到错误的位置。

CODE:

public void setDate(){

        dateView = (TextView) findViewById(R.id.showBirthDate);


        //get birthday
        int id = Integer.parseInt(dbhandler.getLatestProfileId());
        String data=dbhandler.getBirthdate(id);

        //split string for date
        String[] parts = data.split("/");
        month = Integer.parseInt(parts[0]);
        day = Integer.parseInt(parts[1]);
        year = Integer.parseInt(parts[2]);

        //current
        calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, day);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.YEAR, year);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);

        showDate(year, month, day);

    }

1 个答案:

答案 0 :(得分:1)

您可以在初始化期间在日期选择器对话框中设置日期,如下所示:

  datePickerDialog = new DatePickerDialog(
                context,
                null,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
        );