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

时间:2018-11-09 09:02:21

标签: java android date 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获取月份和年份。

谢谢。

2 个答案:

答案 0 :(得分:1)

解决方案::我宁愿建议您使用另一个DatePickerDialog,我目前也在我的项目中使用它。易于使用,请按照以下步骤操作:

第1步:在您的Gradle(应用级别)中添加依赖项:

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

第二步:在您的应用中使用它:

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

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

答案 1 :(得分:0)

尝试

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