当主 class 自行扩展片段时如何使用时间选择器对话框?

How to use time picker dialog when the main class extends a fragment on its own?

当我的主要 class 本身就是一个片段时,我无法理解如何使用时间选择器对话框。

请分享一些代码,以便我解决这个问题。我坚持这个。我需要实施一些其他方法吗?

使用单独的 TimePickerFragment class public class FirstFragment extends Fragment implements TimePickerFragment.TimeDialogListener 会抛出错误: Class 'FirstFragment' 必须声明为抽象方法或在 'TimeDialogListener'

中实现抽象方法 'onFinishDialog(String)'

使用 TimePickerDialog 非常简单

int hour = currentTime.get(Calendar.HOUR_OF_DAY);
int minute = currentTime.get(Calendar.MINUTE);
TimePickerDialog timePicker;
timePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                //logic to handle time set event
            }
        }, hour, minute, true);//Yes 24 hour time
        timePicker.setTitle("Select Time");
        timePicker.show();

Class 'FirstFragment' must either be declared abstract or implement abstract method 'onFinishDialog(String)' in 'TimeDialogListener'

此错误表明您需要重写方法 onFinishDialog(String,因为它在 TimeDialogListener 中声明为 abstract。 只需在 class.

中覆盖它
@Override
public void onFinishDialog(String s){

}

除此之外,您还可以在 class 中直接使用 TimePickerDialog

private void showTimePicker(){
    Calendar calendar =Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    TimePickerDialog timePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
            //Do the stuff here
        }
    }, hour, minute, false);
    timePicker.setTitle("Pick Time");
    timePicker.show();
}