gpt4 book ai didi

java - 子类不调用重写父接口(interface)方法

转载 作者:太空宇宙 更新时间:2023-11-04 12:22:11 25 4
gpt4 key购买 nike

我有一个从 Android 扩展而来的自定义 TimePickerDialog。该对话框有一个接口(interface) 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);
}

}

但它从未被调用过!我怎样才能实现这个目标?

最佳答案

您双方都实现了接口(interface),那么谁在调用onTimeSet?只需在您想要使用此方法的地方实现即可。

关于java - 子类不调用重写父接口(interface)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38744056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com