输入经过的时间(一些对话?)

Enter elapsed time (some Dialog?)

我需要用户输入执行某项任务所花费的时间,例如:01h:35mm:00ss。格式可能会有所不同。

这个任务有没有类似于 DatePickerDialog 的东西?这样,它可以避免引入错误的格式,最重要的是,它对用户来说更优雅、更舒适。

嗯,有内置的 类。请参阅此代码,作为 "quick and dirty" 示例,选择小时和分钟:

static boolean bPickedTimeIsValid = false;

public void showTimePicker ()
{
    bPickedTimeIsValid = false;

    DialogFragment newFragment = new TimePickerFragment ();
    newFragment.show (getFragmentManager (), "timePicker");
}

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)
    {
        // Time has been chosen by the user, do something with it!

        pickedTimeHour = hourOfDay;
        pickedTimeMinutes = minute;
        bPickedTimeIsValid = true;

        // Process the picked time as needed...
    }
}

在您的应用程序中,您将调用 showTimePicker() 并将当用户选择时间时需要执行的代码放入 onTimeSet()

当然,最长持续时间限制为一天。