子类不调用overriden parent的接口方法

时间:2016-08-03 12:43:52

标签: java android

我有一个自定义的TimePickerDialog,它扩展自Android。该对话框有一个接口OnTimeSetListener,必须由其Context或父Fragment实现。

CustomTimePickerDialog.java

public class CustomTimePickerDialog extends DialogFragment implements
        android.app.TimePickerDialog.OnTimeSetListener {

    ...

    private OnTimeSetListener listener;

    public static TimePickerDialog newInstance(int hour, int minutes) {
        TimePickerDialog fragment = new TimePickerDialog();

        return fragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new android.app.TimePickerDialog(getContext(), this, 0, 0, true);
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int hour, int minutes) {
        listener.onTimeSet(hour, minutes);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        Object parent = getParentFragment() == null
                ? context
                : getParentFragment();

        if (parent instanceof OnTimeSetListener) {
            listener = ((OnTimeSetListener) parent);
        } else {
            throw new RuntimeException(parent + " must implement OnTimeSetListener");
        }
    }

    public interface OnTimeSetListener {
        void onTimeSet(int hour, int minutes);
    }
}

问题是我需要再次扩展此类以更改onTimeSet()行为,如下所示:

LimitedCustomTimePickerDialog.java

public class LimitedCustomTimePickerDialog extends CustomTimePickerDialog {

    @Override
    public void onTimeSet(TimePicker timePicker, int hour, int minutes) {
        int standardMinutes = minutes < 30 ? 0 : 30;
        super.onTimeSet(timePicker, hour, standardMinutes);
    }

}

但它永远不会被召唤!我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您是否在双方都实施了界面,以便谁来呼叫onTimeSet?只需实现您想要使用此方法的位置。