DatePickerDialog无法在MaterialLibrary上实现OnDateSetListener

时间:2015-08-10 02:42:07

标签: java android datepickerdialog

我正在尝试使用rey5137's material library中的DatePickerDialog,但它无法实现方法OnDateSetListener。这是我的代码

dateFMCdialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                dateFMC.setText(dateFormatter.format(newDate.getTime()));
            }
        },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

当我实现OnDateSetListener方法时,此代码给出了错误。看起来这个对话框不支持OnDateSetListener。我该怎么做才能从这个对话框中获取价值?谢谢。

1 个答案:

答案 0 :(得分:1)

https://github.com/rey5137/material/blob/master/lib/src/main/java/com/rey/material/widget/DatePicker.java#L430看起来该函数名为setOnDateChangedListener,您需要实现OnDateChangedListener

/**
 * Set the listener will be called when the selected date is changed.
 * @param listener The {@link DatePicker.OnDateChangedListener} will be called.
 */
public void setOnDateChangedListener(OnDateChangedListener listener){
    mOnDateChangedListener = listener;
}

https://github.com/rey5137/material/blob/master/lib/src/main/java/com/rey/material/widget/DatePicker.java#L72是要实现的接口:

/**
 * Interface definition for a callback to be invoked when the selected date is changed.
 */
public interface OnDateChangedListener {

    /**
     * Called when the selected date is changed.
     * @param oldDay The day value of old date.
     * @param oldMonth The month value of old date.
     * @param oldYear The year value of old date.
     * @param newDay The day value of new date.
     * @param newMonth The month value of new date.
     * @param newYear The year value of new date.
     */
    public void onDateChanged(int oldDay, int oldMonth, int oldYear, int newDay, int newMonth, int newYear);
}