4

你好!

当用户单击设置项(首选项)时,我希望我的应用程序显示 TimePicker 对话框。我已经创建了一个创建 TimePicker 对话框的 DialogFragment,但我无法将此片段与自定义首选项联系起来。怎么可能做到这一点?

这是需要与自定义首选项绑定在一起的 DialogFragment 类:

import java.util.Calendar;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public class MyDialogFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        ((OpeningScreen) getActivity()).timerAlert(calendar.getTimeInMillis());

    }
}

PS。我已经阅读了 android 开发者的教程(http://developer.android.com/guide/topics/ui/settings.html),但这对我来说有点太混乱了。非常感谢您的帮助!

4

0 回答 0