我需要用户输入执行特定任务所花费的时间,例如:01h:35mm:00ss。格式可能会有所不同。
此任务是否有类似于DatePickerDialog的内容?这样,它可以避免引入错误的格式,并且最重要的是,它对于用户而言更优雅,更舒适。
答案 0 :(得分:0)
嗯,有内置的类。参见以下代码,作为“快速而肮脏的”示例,以选择小时和分钟:
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()
中选择时间时放置需要执行的代码。
当然,最长持续时间限制为一天。