Block Hours TimePicker

时间:2017-10-20 19:57:41

标签: android google-cloud-firestore android-timepicker

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

它运作良好。

我正在寻找在某些时候限制或阻止访问的方法。

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

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

我需要阻止开始时间前的小时数和结束时间后的小时数。

Code TimePicker:

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

1 个答案:

答案 0 :(得分:0)

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

这是我用于TimePickerDialogFragment的代码。这是为了在某些时间间隔(例如5,10或15分钟)显示TimePicker的分钟段

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

implements TimePickerDialogFragment.EditTimeDialogListener

你可以像这样调用对话框。这个例子将会议记录分为15分钟:

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

initialStartHour是小时我将选择器初始化为例如10上午10点。

设置my 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);
    }

}

现在,您只需在调用活动中提取片段中选择的时间:

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