Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从DatePickerDialog获取月份和年份?_Java_Android_Date_Datepickerdialog - Fatal编程技术网

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

Java 如何从DatePickerDialog获取月份和年份?,java,android,date,datepickerdialog,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);

这是我的代码:

    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;
}
public DatePickerDialog CREATE\u DATEPICKER\u DIALOG(){
int-themeResId=2;
datePickerDialog=新的datePickerDialog(this,themeResId,null,2018,1,1);
datePickerDialog.getDatePicker().setMinDate(获取日历);
datePickerDialog.getDatePicker().setMaxDate(获取日历);
试一试{
//隐藏所选日期。
((ViewGroup)datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier(“day”、“id”、“android”)).setVisibility(View.GONE);
datePickerDialog.updateDate(datePickerDialog.getDatePicker().getYear(),datePickerDialog.getDatePicker().getMonth(),0);
datePickerDialog.setTitle(“变更月”);
datePickerDialog.setButton(DialogInterface.BUTTON_“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
if(which==DialogInterface.BUTTON_正值){

TXT_MONTH.setText(“这里的MONTH_”)解决方案:我建议您使用不同的
DatePickerDialog
,我目前也在我的项目中使用它。它很容易使用,请按照以下步骤操作:

步骤1:在你的Gradle中添加依赖项(应用程序级别):

第二步:在应用程序中使用它:

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());

什么东西不起作用?你能发布你的datePickerDialog的代码吗?我无法获取所选月份和年份的值What is months?字符串months[]={“一月”、“二月”、“等等”}
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;
}