How to disable future date picker in android

后端 未结 4 607
情话喂你
情话喂你 2020-12-22 13:27

I\'m new to android How to do disable future date in date picker, I tried many codes in stack over flow but it couldn\'t help.can anybody help me pleas.

Have a look

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 14:09

     mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
    
    
    ed_date.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Calendar mcurrentDate=Calendar.getInstance();
                    year=mcurrentDate.get(Calendar.YEAR);
                    month=mcurrentDate.get(Calendar.MONTH);
                    day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
    
                    final DatePickerDialog   mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
                    {
                        @Override
                        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
                        {
                                  ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                                int month_k=selectedmonth+1;
    
                        }
                    },year, month, day);
                    mDatePicker.setTitle("Please select date");
                    // TODO Hide Future Date Here
                    mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
    
                    // TODO Hide Past Date Here
                    //  mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
                    mDatePicker.show();
                }
            }); 
    

    Its Working

提交回复
热议问题