如何在android中的timepicker上禁用过去的时间

时间:2018-03-09 11:00:34

标签: android datepicker timepicker

这是我的代码时间视图和日期视图。我将如何选择日期然后设置当前时间并禁用过去时间..下一个日期将选择所有时间将启用如何可能这些选项如何编写此代码,任何人帮助,如何按时禁用过去的时间Android中使用日历的选择器

public  void CalenderView() {

          /* ends after 1 month from now */
            Calendar endDate = Calendar.getInstance();
            endDate.add( Calendar.DAY_OF_WEEK_IN_MONTH, 1 );

            HorizontalCalendar horizontalCalendar = new HorizontalCalendar.Builder( this, R.id.calendarView )
                    .range( Calendar.getInstance(), endDate )
                    .datesNumberOnScreen( 5 )
                    .configure()
                    .formatTopText( "EEE" )
                    .formatMiddleText( "dd" )
                    .showBottomText( false )
                    .end()
                    .build();

            horizontalCalendar.getConfig()
                    .setSizeTopText( (float) 12 )
                    .setSizeMiddleText( (float) 25 );

            horizontalCalendar.setCalendarListener( new HorizontalCalendarListener() {
                @Override
                public void onDateSelected(Calendar date, int position) {

                }
            } );
        }




   public void TimePickerView(){


            //Get the widgets reference from XMLayout
            final TextView tv = (TextView) findViewById(R.id.tv);
             tp = (TimePicker) findViewById(R.id.tp);
            Button btn = (Button) findViewById(R.id.btn);

            //Set the TextView text color
                tv.setTextColor(Color.parseColor("#b601fd"));

                btn.setOnClickListener(new View.OnClickListener() {



                @Override
                public void onClick(View v) {
                    int hourOfDay = tp.getCurrentHour(); //Get TimePicker current hour
                    int minute = tp.getCurrentMinute(); //Get TimePicker current minute

                    //Display the TimePicker current time to app interface
                    String AMPM = "AM";
                    if(hourOfDay>11)
                    {
                        //Get the current hour as AM PM 12 hour format
                        hourOfDay = hourOfDay-12;
                        AMPM = "PM";
                    }
                    tv.setText("" + hourOfDay + ":" + minute + ":" + AMPM);
                }
            });

2 个答案:

答案 0 :(得分:0)

如果你想要禁用传递日期,那么在下面的代码中使用datepicker视图..

datePicker.setMinDate(System.currentTimeMillis() - 1000);

然后使用DatePickerDialog然后在代码下面使用...

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

并且您在两种方法下使用的时间和日期不会显示...

    /**
 * to display date picker dialog
 */
private void showDatePicker() {
    if (mEventDatePickerDialog == null) {
        if (newCalendar == null) {
            newCalendar = Calendar.getInstance();
        }
        mEventDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                newCalendar.set(Calendar.YEAR, year);
                newCalendar.set(Calendar.MONTH, monthOfYear);
                newCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                // newCalendar.setTimeInMillis(System.currentTimeMillis());
                showTimePicker();

                //mTvEventDate.setText(dateFormatter.format(newDate.getTime()));
            }
        }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
        mEventDatePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        mEventDatePickerDialog.show();
    } else if (mEventDatePickerDialog != null && !mEventDatePickerDialog.isShowing()) {
        if (newCalendar == null) {
            newCalendar = Calendar.getInstance();
        }
        mEventDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                newCalendar.set(Calendar.YEAR, year);
                newCalendar.set(Calendar.MONTH, monthOfYear);
                newCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                // newCalendar.setTimeInMillis(System.currentTimeMillis());
                showTimePicker();
                //mTvEventDate.setText(dateFormatter.format(newDate.getTime()));
            }
        }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
        mEventDatePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        mEventDatePickerDialog.show();
    }


}

/**
 * to display time picker dialog
 */
private void showTimePicker() {

    if (newCalendar == null) {
        newCalendar = Calendar.getInstance();
    }

    mEventTimePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            Calendar datetime = Calendar.getInstance();
            Calendar c = Calendar.getInstance();
            datetime.set(Calendar.MONTH, newCalendar.get(Calendar.MONTH));
            datetime.set(Calendar.DAY_OF_MONTH, newCalendar.get(Calendar.DAY_OF_MONTH));
            datetime.set(Calendar.YEAR, newCalendar.get(Calendar.YEAR));
            datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            datetime.set(Calendar.SECOND, 00);
            datetime.set(Calendar.MINUTE, minute);
            mSelectDate = new Date(datetime.getTimeInMillis());
            Date mCurrentDate = new Date(c.getTimeInMillis());

            if (mSelectDate.after(mCurrentDate)) {
                newDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
                newDate.set(Calendar.MINUTE, minute);
                newDate.set(Calendar.SECOND, 00);
                newCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                newCalendar.set(Calendar.MINUTE, minute);
                newCalendar.set(Calendar.SECOND, 00);
                mTvEventDate.setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
                mTvEventDate.setText(dateFormatter.format(newDate.getTime()));
            } else {
                //((BaseActivity) getActivity()).showAlertDialog(getActivity(), getString(R.string.app_name), getString(R.string.time_validation));
                Toast.makeText(this,"your msg",Toast.LENGTH_SHORT).show();
                mTvEventDate.setText(getString(R.string.yyyy_mm_dd_hh_mm_ss));
                mTvEventDate.setTextColor(ContextCompat.getColor(getActivity(), R.color.create_event_hint_color));
            }
        }
    }, newCalendar.get(Calendar.HOUR_OF_DAY), newCalendar.get(Calendar.MINUTE), false);

    mEventTimePickerDialog.show();
}

答案 1 :(得分:-2)

如果您使用的是Android DatePicker,则可以将最短日期设置为今天的日期,因此不会启用今天之前的日期。 您可以使用此代码段:

datePicker.setMinDate(new Date().getTime());

否则,如果您使用的是DatePickerDialog:

datePickerDialog.getDatePicker().setMinDate(new Date().getTime());