1
我有一個自定義的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);
}
}
但它從來沒有calle d!我怎樣才能做到這一點?
只有超類實現它,子類覆蓋了它的方法。 'onTimeSet'被原生的Android TimePickerDialog調用,當用戶點擊對話框的「OK」按鈕時就會發生這種情況。 – josemigallas