DatePickerDialog显示随机数而不是选择月份

时间:2017-10-08 14:24:20

标签: java android date datepickerdialog

我使用DatePickerDialog来选择日期并在EditText字段中显示它,我对departureDateEditText字段和returnDateEditText字段都做同样的事情。除了从DatePicker中选择日期时显示的月份,一切似乎都有效。它不是返回所选月份,而是返回一个随机数(通常在01到50之间)代替月份。使用的date_template是" dd / mm / yyyy"。以下是我的代码,任何有关如何解决这个问题的帮助将不胜感激。我已经看过很多其他的解决方案,但我找不到任何有效的方法。

    final Calendar departureCalendar = Calendar.getInstance();
    final Calendar returnCalendar = Calendar.getInstance();
    final String myFormat = getResources().getString(R.string.date_template);
    final SimpleDateFormat dateFormat = new SimpleDateFormat(myFormat, Locale.ENGLISH);

    final DatePickerDialog.OnDateSetListener departureDate = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            departureCalendar.set(Calendar.YEAR, year);
            departureCalendar.set(Calendar.MONTH, month);
            departureCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            departureDateEditText.setText(dateFormat.format(departureCalendar.getTime()));
        }
    };

    final DatePickerDialog.OnDateSetListener returnDate = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            returnCalendar.set(Calendar.YEAR, year);
            returnCalendar.set(Calendar.MONTH, month);
            returnCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            returnDateEditText.setText(dateFormat.format(returnCalendar.getTime()));
        }
    };

    departureDateEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(MainActivity.this, departureDate, departureCalendar.get(Calendar.YEAR),
                    departureCalendar.get(Calendar.MONTH), departureCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

    returnDateEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(MainActivity.this, returnDate, returnCalendar.get(Calendar.YEAR),
                    returnCalendar.get(Calendar.MONTH), returnCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

0 个答案:

没有答案