在Lollipop版本中,DatePickerDialog.OnDateSetListener失败了,任何解决方案?

时间:2016-09-23 11:45:48

标签: android

月和年微调器创建对话框

private DatePickerDialog createDialogWithoutDateField() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dpd = new DatePickerDialog(this, DatePickerDialog.THEME_HOLO_LIGHT, monthSetListener, year, monthOfYear, dayOfMonth);

        LinearLayout pickerParentLayout = (LinearLayout) dpd.getDatePicker().getChildAt(0);
        LinearLayout pickerSpinnersHolder = (LinearLayout) pickerParentLayout.getChildAt(0); // first child
        NumberPicker picker = (NumberPicker) pickerSpinnersHolder.getChildAt(0);
        picker.setVisibility(View.GONE);


        return dpd;
    }

监听器声明为

private DatePickerDialog.OnDateSetListener monthSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
            Log.d("result", " month: " + selectedMonth + " year: " + selectedYear);
        }
    };

日期输出不适用于月份微调器或年份微调器的更改。有什么想法吗?

4 个答案:

答案 0 :(得分:0)

根据Android code OnDateSetListener的方法:

void onDateSet(DatePicker view, int year, int month, int dayOfMonth);
仅当点击DatePickerDialog的正面按钮时,才会调用

你可能会做的是引用NumberPicker内的每个DatePickerDialog(就像你隐藏小时选择器一样)并设置它OnValueChangeListener并做你想做的事内部监听器的方法:

void onValueChange(NumberPicker picker, int oldVal, int newVal);

答案 1 :(得分:0)

第1步:

在您的活动中实施以下界面 DatePickerDialog.OnDateSetListener

第2步:

然后您将获得override方法

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}

第3步:

使用以下方法显示弹出窗口

private void loadCalendar() {
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog df = new DatePickerDialog(OptionCalculator.this, this, year, month, day);
    df.show();
}

它会正常工作。祝你好运:)

答案 2 :(得分:0)

这是Android 5.0及更高版本中的一个错误,当日期选择器处于新材料样式日历模式时会发生这种错误。 您可以通过在DatePicker上设置android:datePickerMode="spinner"来强制datepicker使用5.0之前的微调器模式来解决此错误。

或者您可以使用DatePickerDialog代替。即使对话框处于新材料日历模式,也会调用对话框侦听器的onDateChanged方法。

答案 3 :(得分:0)

以下是将要解决的代码。

final Calendar mcurrentDate = Calendar.getInstance();
        int mYear = mcurrentDate.get(Calendar.YEAR);
        int mMonth = mcurrentDate.get(Calendar.MONTH);
        int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog mDatePicker = new DatePickerDialog(
                MainActivity.this, new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker datepicker,
                                  int selectedyear, int selectedmonth,
                                  int selectedday) {

                mcurrentDate.set(Calendar.YEAR, selectedyear);
                mcurrentDate.set(Calendar.MONTH, selectedmonth);
                mcurrentDate.set(Calendar.DAY_OF_MONTH,
                        selectedday);
                SimpleDateFormat sdf = new SimpleDateFormat(
                        "MMM dd, yyyy",
                        Locale.US);


                edtTxtDOB.setText(sdf.format(mcurrentDate
                        .getTime()));
            }
        }, mYear, mMonth, mDay);

        mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
        mDatePicker.show();