How to set Maxmum and minimum time in Time picker?

半腔热情 提交于 2019-12-22 08:34:44

问题


i have time interval 12:00 PM to 12:00 AM.I want set this time interval in time picker dialog.Disable the time before 12:00 PM and after 12:00 AM in time picker.I don't know how to set time in time picker dialog.Can any one know help me to solve this issue.

Time Picker

 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();
           }
    }
};

Time Picker Dialog Fragment

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:


I'm not sure if you can change what time values are presented in the time picker, you can however create an event listener for when the timepicker changes that will reject any values not in the interval you want



来源:https://stackoverflow.com/questions/24029949/how-to-set-maxmum-and-minimum-time-in-time-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!