从PreferenceFragment活动中显示TimePicker对话框

时间:2013-05-01 17:52:44

标签: java android

嗨!

我希望我的应用程序在用户单击设置项(Preference)时显示TimePicker对话框。我已经创建了一个DialogFragment来创建一个TimePicker对话框,但是我无法将这个片段与自定义首选项绑定。怎么可以这样做?

这是需要与自定义首选项绑定在一起的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),但这对我来说有点太混乱了。非常感谢你的帮助!

0 个答案:

没有答案