5

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

时间选择器

 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;
}
}
4

1 回答 1

0

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

于 2014-06-04T05:53:31.327 回答