繁体   English   中英

如何从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(应用级别)中添加依赖项:

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()得到月()。]); TXT_YEAR.setText(Integer.toString(datePickerDialog.getDatePicker()得到年())。);

暂无
暂无

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

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