如何在Timepicker中设置最长和最短时间?

时间:2014-06-04 05:49:37

标签: android android-dialogfragment android-timepicker

我有时间间隔12:00 PM到12:00 AM。我想在时间选择器对话框中设置这个时间间隔。禁用时间选择器的12:00 PM之前和12:00之后的时间。我不知道知道如何设置时间选择对话框。任何人都知道帮助我解决这个问题。

时间选择器

 private void showTimePicker(){

      Time_picker time = new Time_picker();

      Bundle args = new Bundle();
      args.putInt("hours", c.get(Calendar.HOUR_OF_DAY)+2);
      args.putInt("minute", c.get(Calendar.MINUTE)+30);

      time.setArguments(args);
      time.setCallBack(ontime);
      time.show(((FragmentActivity) getActivity()).getSupportFragmentManager(), "Time Picker");

 }

 OnTimeSetListener ontime = new OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub

        try {       
               String _24HourTime = String.valueOf(hourOfDay)+":"+String.valueOf(minute);
               SimpleDateFormat _24HourSDF = new SimpleDateFormat("HH:mm");
               SimpleDateFormat _12HourSDF = new SimpleDateFormat("hh:mm a");
               Date _24HourDt = _24HourSDF.parse(_24HourTime);
               et_time.setText(_12HourSDF.format(_24HourDt));
           } catch (Exception e) {
               e.printStackTrace();
           }
    }
};

时间选择器对话框片段

public class Time_picker extends DialogFragment{

OnTimeSetListener ontimeSet;

public Time_picker() {

}

public void setCallBack(OnTimeSetListener ontime) {

    ontimeSet = ontime;

}

private int hours, minute, pm;
@Override
public void setArguments(Bundle args) {
    super.setArguments(args);
    hours = args.getInt("hours");
    minute = args.getInt("minute");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    TimePickerDialog _time = new TimePickerDialog(getActivity(), ontimeSet, hours, minute, false){

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub


        }
    };

    return _time;
}
}

1 个答案:

答案 0 :(得分:0)

我不确定您是否可以更改时间选择器中显示的时间值,但是您可以为时间戳更改时创建一个事件侦听器,以拒绝不在您想要的时间间隔内的任何值