块时间 TimePicker

Block hours TimePicker

我有一个字段可以通过 TimerPicker 选择时间。

一切正常。

我正在寻找一种在特定时间限制或阻止访问的方法。

例如: 打开和关闭时间的字段,保存在 FireStore (Firebase) 中:

- Start time: 08:00
- HourEnd: 7:00 p.m.

我需要屏蔽开始时间之前的时间和结束时间之后的时间。

代码时间选择器:

private void timePicker(){

        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                        mHour = hourOfDay;
                        mMinute = minute;


                        /*Formato da hora*/
                        String curTime = String.format("%02d:%02d", hourOfDay, minute);

                        mHora.setText(curTime);
                        mHoraString = curTime;

                    }
                }, mHour, mMinute, true);
        timePickerDialog.show();
}

我希望这能让您了解如何实现您正在寻找的东西。我意识到这不是您正在寻找的确切解决方案,但这是一个开始。

这是我用于 TimePickerDialogFragment 的代码。这旨在显示 TimePicker 特定时间间隔的分钟段(例如 5、10 或 15 分钟)

在将调用 TimePickerDialogFragment 的 activity 中,您需要实现回调侦听器:

implements TimePickerDialogFragment.EditTimeDialogListener

你这样调用对话框。此示例将分钟设置为 15 分钟间隔:

    TimePickerDialogFragment picker = TimePickerDialogFragment.newInstance("Select a Time", initialStartHour, 15);
    picker.show(getFragmentManager(), "TimePickerDialogFragment");

initialStartHour 是我初始化选择器的时间,例如 10 表示上午 10 点。

我的 TimePickerDialogFragment 的代码设置为按时间间隔设置分钟数。

public class TimePickerDialogFragment extends DialogFragment {

    private static final String ARG_TITLE = "title";
    private static final String ARG_START_HOUR = "startHour";
    private static final String ARG_MINUTE_INTERVAL = "minute_interval";

    private static final DecimalFormat FORMATTER = new DecimalFormat("00");
    NumberPicker minutePicker;

    private String title;
    private int startHour;
    private int minuteInterval;
    private String selectedTime;

    private EditTimeDialogListener mListener;

    public TimePickerDialogFragment() {
    }

    public static TimePickerDialogFragment newInstance(String title, int startHour, int minuteInterval) {
        TimePickerDialogFragment fragment = new TimePickerDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putInt(ARG_START_HOUR, startHour);
        args.putInt(ARG_MINUTE_INTERVAL, minuteInterval);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_TITLE);
            startHour = getArguments().getInt(ARG_START_HOUR);
            minuteInterval = getArguments().getInt(ARG_MINUTE_INTERVAL);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle saveIntsanceState){

        final Context context = getActivity();

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View rootView = inflater.inflate(R.layout.fragment_time_picker_dialog, null, false);
        final TimePicker picker = (TimePicker) rootView.findViewById(R.id.timePicker);
        View minute = picker.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));
        setMinutePicker(minute);

        //Block number keyboard
        picker.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

        picker.setCurrentHour(startHour);
        picker.setCurrentMinute(0);
        // setHour is available for API level 23 and above!!
//        picker.setHour(startHour);

        final TextView tvTitle = (TextView)rootView.findViewById(R.id.tvTimePickerDialogTitle);
        tvTitle.setText(title);
//        picker.setSpinnersShown(false);
//        picker.setCalendarViewShown(true);

        builder.setView(rootView)
        // setTitle is available for API level 23 and above!!
//                .setTitle(title)
                .setPositiveButton(R.string.ok_button_dialog_title, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // call the method on the parent activity when user click the positive button

                        Integer h = picker.getCurrentHour();
                        Integer m = picker.getCurrentMinute() * minuteInterval;

                        String hh = h.toString();
                        if(h < 10){
                            hh = "0" + hh;
                        }

                        String mm = m.toString();
                        if(m < 10){
                            mm = "0" + mm;
                        }

                        selectedTime = hh + mm;
                        if(mListener == null) mListener = (EditTimeDialogListener) context;
                        mListener.onFinishTimeDialog(selectedTime);
                    }
                })
                .setNegativeButton(R.string.cancel_button_dialog_title, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // call the method on the parent activity when user click the negative button
                    }
                });
        return builder.create();
    }

    private void setMinutePicker(View minute){
        int n = 60/minuteInterval;
        String[] displayValues = new String[n];

        for(int i = 0; i < n; i++){
            displayValues[i] = FORMATTER.format(i*minuteInterval);
        }

        if((minute != null) && (minute instanceof NumberPicker)){
            minutePicker = (NumberPicker) minute;

            minutePicker.setMinValue(0);
            minutePicker.setMaxValue(n-1);
            minutePicker.setDisplayedValues(displayValues);
        }
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            if(mListener == null) mListener = (EditTimeDialogListener) context;
        }
        catch (Exception ex){
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface EditTimeDialogListener {
        void onFinishTimeDialog(String selectedTime);
    }

}

现在您所要做的就是在您的调用中提取在片段中选择的时间 activity:

@Override
public void onFinishTimeDialog(String selectedTime) {
        // Now set the TextView text!!
        mHora.setText(selectedTime);
}