How to set minimum DatePicker date to current date

前端 未结 9 1750
有刺的猬
有刺的猬 2020-11-28 07:30

I want to set the minimum date the user can choose in a DatePicker to the current date. I\'ve tried this:

DatePicker datePicker = (DatePicker) findViewById(R         


        
9条回答
  •  青春惊慌失措
    2020-11-28 07:48

    Check the Android DatePickerDialog set minimum and maximum date code.

    Here is the examples.

    final Calendar c = Calendar.getInstance();  
        int year = c.get(Calendar.YEAR);  
        int month = c.get(Calendar.MONTH);  
        int day = c.get(Calendar.DAY_OF_MONTH);  
    DatePickerDialog dpd = new DatePickerDialog(getActivity(),AlertDialog.THEME_TRADITIONAL,this,year, month, day);  
    
        //Get the DatePicker instance from DatePickerDialog  
        DatePicker dp = dpd.getDatePicker();  
        //Set the DatePicker minimum date selection to current date  
        dp.setMinDate(c.getTimeInMillis());//get the current day  
        //dp.setMinDate(System.currentTimeMillis() - 1000);// Alternate way to get the current day  
    
        //Add 6 days with current date  
        c.add(Calendar.DAY_OF_MONTH,6);  
    
        //Set the maximum date to select from DatePickerDialog  
        dp.setMaxDate(c.getTimeInMillis());  
        //Now DatePickerDialog have 7 days range to get selection any one from those dates 
    

提交回复
热议问题