如何从 DatePickerDialog 获取月份和年份?

How to get the month and year from the DatePickerDialog?

这是我的代码:

    public DatePickerDialog CREATE_DATEPICKER_DIALOG() {
        int themeResId = 2;
        datePickerDialog = new DatePickerDialog(this, themeResId, null, 2018, 1, 1);
        datePickerDialog.getDatePicker().setMinDate(get_min_calendar);
        datePickerDialog.getDatePicker().setMaxDate(get_max_calendar);

        try {
            // Hide selected day.
            ((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
            datePickerDialog.updateDate(datePickerDialog.getDatePicker().getYear(), datePickerDialog.getDatePicker().getMonth(), 0);
            datePickerDialog.setTitle("Change Month");
            datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(which==DialogInterface.BUTTON_POSITIVE){
                    TXT_MONTH.setText("MONTH_HERE");<----- datePickerDialog.getDatePicker().getMonth();
                    TXT_YEAR.setText("YEAR_HERE");<-----datePickerDialog.getDatePicker().getYear();
                    }else{

                    }
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        return datePickerDialog;
}

我用了datePickerDialog.getDatePicker().getMonth();datePickerDialog.getDatePicker().getYear();

但是没用。任何形式的帮助将不胜感激。我只想使用上面的 DatePickerDialog 获取月份和年份。

提前致谢。

解决方案: 我宁愿建议您使用不同的 DatePickerDialog,我目前也在我的项目中使用它。简单易用,按以下步骤操作:

步骤 1: 在你的 Gradle (app level) 添加依赖:

implementation('com.wdullaer:materialdatetimepicker:3.3.0') {
    exclude group: 'com.android.support'
}

步骤 2: 在您的应用中使用它:

try {
    Calendar now = Calendar.getInstance();

    DatePickerDialog dpd = DatePickerDialog.newInstance(

            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
                    .......
                    // Get Year, Month and Day from the parameter here when you select and date.
                    .......
                }
            },
            now.get(Calendar.YEAR),
            now.get(Calendar.MONTH),
            now.get(Calendar.DAY_OF_MONTH)
    );
    dpd.setAccentColor(ContextCompat.getColor(FilterForLoadBoardActivity.this, R.color.colorPrimary));
    dpd.setMinDate(now);
    dpd.show(getFragmentManager(), "Datepickerdialog");
    return;
} catch (Exception e) {
    e.printStackTrace();
}

最后: 您的方法应该如下所示:

public DatePickerDialog CREATE_DATEPICKER_DIALOG() {
......
......
......
DatePickerDialog dpd;

try {
Calendar now = Calendar.getInstance();

dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
             @Override
             public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
                 .......
                 // Get Year, Month and Day from the parameter here when you select and date.
                 .......
                 }
             },
             now.get(Calendar.YEAR),
             now.get(Calendar.MONTH),
             now.get(Calendar.DAY_OF_MONTH)
         );
        dpd.setAccentColor(ContextCompat.getColor(FilterForLoadBoardActivity.this, R.color.colorPrimary));
        dpd.setMinDate(now);
        dpd.show(getFragmentManager(), "Datepickerdialog");
     } catch (Exception e) {
        e.printStackTrace();
     }
     return dpd;
}

尝试一下,希望对您有所帮助。

试试这个

TXT_MONTH.setText(月[datePickerDialog.getDatePicker().getMonth()]); TXT_YEAR.setText(Integer.toString(datePickerDialog.getDatePicker().getYear()));