我有一个名为DatePicker的自定义类,该类扩展了DialogFragment并实现了DatePickerDialog.OnDateSetListener,但是我的问题是因为y需要监听onDateChangeMethod,而DialogFragment仅具有DialogInterface.OnCancelListenerDialogInterface.OnDismissListener

我怎样才能做到这一点?我尝试实现OnDateChangedListener并覆盖了onDateChange方法,但是它不起作用。这是代码:

public class DatePicker extends DialogFragment implements
        DatePickerDialog.OnDateSetListener, OnDateChangedListener {

    private OnDateSetListener _onDateSetListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        _onDateSetListener = (OnDateSetListener) activity;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Date initialValue = null;
        String[] buttons = getArguments().getStringArray("buttons");

        Long initialValueLong = getArguments().getLong("initialValue");
        if (initialValue == null) {
            initialValue = new Date();
        }

        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTimeInMillis(initialValueLong);

        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this,
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DATE));


        return dialog;
    }

     @Override
     public void onDateSet(android.widget.DatePicker view, int year,
           int monthOfYear, int dayOfMonth) {
           _onDateSetListener.onDateSet(new GregorianCalendar(year, monthOfYear,
               dayOfMonth).getTime(), getArguments().get("tag"));
     }

    @Override
    public void onDateChanged(android.widget.DatePicker view, int year,
            int monthOfYear, int dayOfMonth) {

        // THIS METHOD NEVER CALLED

    }
}

最佳答案

看来您尚未在类中声明onDateSetListener,并且还将主机活动转换为错误的类型。 Dialog类本身应为OnDateSetListener,并且主机活动应强制转换为其他内容。

尝试像这样构造类:

public class DatePickerDialogFragment extends DialogFragment implements
                                             DatePickerDialog.OnDateSetListener
{
    public static interface DatePickedListener
    {
        public void onDatePicked(int selectedYear, int selectedMonth, int selectedDay);
    }

    private DatePickedListener listener;

    @Override
    public void onAttach(Activity activity)
    {
        // when the fragment is initially attached to the activity, cast the
        // activity to the callback interface type
        super.onAttach(activity);

        try
        {
            listener = (DatePickedListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + " must implement " + DatePickedListener.class.getName());
        }
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        Bundle b = getArguments();

        int year = b.getInt("set_year");
        int month = b.getInt("set_month");
        int day = b.getInt("set_day");

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }


    @Override
    public void onDateSet(DatePicker view, int setYear, int setMonth, int setDay)
    {
        // when the date is selected, send it to the activity via its callback interface method

        listener.onDatePicked(setYear, setMonth, setDay);
    }
}

08-18 20:39