如何设置TimePickerDialog的最大和最小时间限制,供用户在特定小时范围内选择?

时间:2018-04-09 15:07:52

标签: android

我创建了一个普通的TimePickerDialog,它从用户那里获取时间输入。但我想改变它,以便它只接受最小和最大小时值内的小时。我想动态设置最小值和最大值(在创建时通过参数发送或以其他方式发送)

public class TimePickerDialogFragment extends DialogFragment {

public static final String EXTRA_TIME = "com.android.XYZ.time";


private static final String ARG_TIME = "time";

private TimePicker mTimePicker;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return null;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){

    View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_time, null);

    mTimePicker = (TimePicker) v.findViewById(R.id.dialog_time_picker);


    return new AlertDialog.Builder(getActivity()).
            setView(v).
            setTitle("Choose a time").
            setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(Build.VERSION.SDK_INT < 23){
                        int getHour = mTimePicker.getCurrentHour();
                        int getMinute = mTimePicker.getCurrentMinute();
                        int getSecond = 0;

                        Time time = new Time(getHour,getMinute,getSecond);

                        //Date date = new GregorianCalendar(year, month, day).getTime();
                        sendResult(Activity.RESULT_OK, time);
                    } else{
                        int getHour = mTimePicker.getHour();
                        int getMinute = mTimePicker.getMinute();
                        int getSecond = 0;

                        Time time = new Time(getHour,getMinute,getSecond);

                        //Date date = new GregorianCalendar(year, month, day).getTime();
                        sendResult(Activity.RESULT_OK, time);

                    }

                }
            }).create();

}


/**
 * To send the chosen time result to Required class thorough intent
 * @param resultCode
 * @param time
 */
private void sendResult(int resultCode, Time time){
    if(getTargetFragment() == null){
        return;
    }

    Intent intent = new Intent();
    intent.putExtra(EXTRA_TIME, time);

    getTargetFragment().onActivityResult(getTargetRequestCode(),resultCode,intent);
}


/**
 * Creating new instance of Time Picker
 * @param time
 * @return
 */
public static TimePickerDialogFragment newInstance(Time time){
    Bundle args = new Bundle();
    args.putSerializable(ARG_TIME, time);

    TimePickerDialogFragment fragment = new TimePickerDialogFragment();
    fragment.setArguments(args);
    return fragment;
}
}

1 个答案:

答案 0 :(得分:0)

您可以设置TimeChangedListener并覆盖其onTimeChanged方法,使用setHour将小时设置为最接近的允许小时数。
例如,这将强制选定的小时在12(12pm)和15(下午3:00)之间:

mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) {
            if(hourOfDay<12) {
                timePicker.setHour(12);
            }
            else if(hourOfDay>15) {
                timePicker.setHour(15);
            }
        }
    });