Android - 如何从时间选择器中获取值?

时间:2013-10-15 01:25:57

标签: android

纯粹从本教程(http://developer.android.com/guide/topics/ui/controls/pickers.html)开始,我可以轻松地显示选择器,但不知道如何从中获取实际值。

public static class TimePickerFragment 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
    }
}

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}

2 个答案:

答案 0 :(得分:0)

public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // Do something with the time chosen by the user }

此功能将具有所选值。

您可以从TimePicker

获得其他感兴趣的功能

答案 1 :(得分:0)

hour =timePicker.getCurrentHour();
minute =timePicker.getCurrentMinute();