我已在TimePickerFragment
&的帮助下整合DialogFragment
TimePickerDialog.OnTimeSetListener
。当我点击TextView
时,它会调用/打开此TimePickerFragment
。
成功案例:
1]当我随时滚动时,例如11:19&按OK(TimePicker)然后设置正确的时间11:19
2]当我点击时间(小时/分钟)并使用软键盘编辑时,例如04:10&然后按下软键盘ENTER按钮&然后OK(TimePicker)然后它设置正确的时间04:10
案例失败:
1]当我点击时间(小时/分钟)&使用软键盘编辑,例如08:44&然后 不 按下软键盘按钮&然后确定(TimePicker)然后IT设置错误的时间任何更早的设置时间 例如10:35或其他什么
以下是我的代码:
//inside on click
case R.id.textViewEtoa:
DialogFragment newFragment = new TimePickerFragment(getActivity(), this);
newFragment.show(getActivity().getSupportFragmentManager(), "timePicker");
break;
//inside TimePickerFragment class
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
private Context context;
private OnTimeSetListener onTimeSetListener;
public TimePickerFragment(Context context, OnTimeSetListener onTimeSetListener) {
// TODO Auto-generated constructor stub
this.context = context;
this.onTimeSetListener = onTimeSetListener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
int hour, minute;
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
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
System.out.println(minute + " ...hhour.... " + hourOfDay);
onTimeSetListener.onTimeSet((hourOfDay < 10 ? "0" + hourOfDay : "" + hourOfDay),
(minute < 10 ? "0" + minute : "" + minute));
}
}