2014-05-20 239 views
34

如何禁用Android日期選擇器中的過去日期?如何禁用Android日期選擇器中的過去日期?

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DATE_DIALOG_ID: 
     // set date picker as current date 
     return new DatePickerDialog(this, datePickerListener, year, month, 
       day); 
    } 
    return null; 
} 

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { 
    public void onDateSet(DatePicker view, int selectedYear, 
      int selectedMonth, int selectedDay) { 
     year = selectedYear; 
     month = selectedMonth+1; 
     day = selectedDay; 

     startdate.setText(new StringBuilder().append(day).append("-") 
       .append(getMonth(month + 1)).append("-").append(year) 
       .append(" ")); 
    } 
}; 
+0

DatePickerDialog datePickerDialog =新DatePickerDialog(getActivity(),日期,myCalendar.get(Calendar.YEAR) myCalendar.get(的Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)); datePickerDialog.getDatePicker()。setMinDate(System.currentTimeMillis() - 1000); datePickerDialog.show(); – Dilip

回答

121

你可以做

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

這臺今天的日期作爲最小的日期和所有過去的日期將被禁用。

datePickerDatePicker一個對象,如果你正在使用的DatePickerDialog一個對象,你可以做

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

注:setMinDate在API推出了11

+0

感謝您的迴應..我應該把這條線放在哪裏? – Addy

+0

你在哪裏創建'DatePicker'或'DatePickerDialog'對象 – Apoorv

+1

謝謝你的回答。但是當我使用上述解決方案時,它使我在應用程序中崩潰。所以我將「System.currentTimeMillis() - 1000」更改爲「System.currentTimeMillis() - 10000」,我的應用程序運行良好。你可以請lemme知道爲什麼我崩潰在減去1000,但不是在對我有效的10000 – user3705697

6

這是我要做的事:

public class DatePickerFragment extends DialogFragment { 
OnDateSetListener ondateSet; 
Calendar c; 
int year = 0, month = 0, day = 0; 

public DatePickerFragment() { 
} 

public void setCallBack(OnDateSetListener ondate) { 
    ondateSet = ondate; 
} 

public static DatePickerFragment newInstance(Bundle bundle) { 
    DatePickerFragment myFragment = new DatePickerFragment(); 
    myFragment.setArguments(bundle); 
    return myFragment; 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // final Calendar c = Calendar.getInstance(); 
//if else for null arguments 
    if (getArguments() != null) { 
     year = getArguments().getInt("year"); 
     month = getArguments().getInt("month"); 
     day = getArguments().getInt("day"); 
     c = Calendar.getInstance(); 
     c.set(year, month, day); 

    } else { 
     c = Calendar.getInstance(); 
     year = c.get(Calendar.YEAR); 
     month = c.get(Calendar.MONTH); 
     day = c.get(Calendar.DAY_OF_MONTH); 
     Log.d("else", "else"); 
    } 


    DatePickerDialog picker = new DatePickerDialog(getActivity(), 
      ondateSet, year, month, day); 
    picker.getDatePicker().setMinDate(c.getTime().getTime()); 
    Log.d("picker timestamp", c.getTime().getTime() + ""); 
    return picker; 
} 
} 

這就是你如何實例化選擇器:

Bundle bundle = new Bundle(); 
     Calendar c = Calendar.getInstance(); 
     bundle.putInt("year", c.get(Calendar.YEAR)); 
     bundle.putInt("month", c.get(Calendar.MONTH)); 
     bundle.putInt("day", c.get(Calendar.DAY_OF_MONTH)); 
     DatePickerFragment fragment = DatePickerFragment 
       .newInstance(bundle); 
     fragment.setCallBack(dateSet); 
     fragment.show(getActivity().getSupportFragmentManager(), null); 

這是我的實施隨時可以改變它。請注意,此代碼默認將mindate設置爲currnet日期。 newInstance也很重要。

+1

那一個我期待的..謝謝。 –

11

此方法將正常工作。

//Get yesterday's date 
Calendar calendar = Calendar.getInstance(); 
calendar.add(Calendar.DATE, -1); 

//Set yesterday time milliseconds as date pickers minimum date 
DatePickerDialog datePickerDialog = new DatePickerDialog(context, myDateListener, year, month, day); 
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis()); 
datePickerDialog.show(); 
1

您可以使用此行到你的代碼...

private Calendar cal; 
private int day; 
private int month; 
private int year; 

static final int DATE_PICKER_ID = 1111; 
TextView textView; 

    textView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      cal = Calendar.getInstance(); 
      day = cal.get(Calendar.DAY_OF_MONTH); 
      month = cal.get(Calendar.MONTH); 
      year = cal.get(Calendar.YEAR); 
      showDialog(DATE_PICKER_ID); 

     } 
    }); 

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case DATE_PICKER_ID: 
      // create a new DatePickerDialog with values you want to show 

      DatePickerDialog datePickerDialog = new DatePickerDialog(this, datePickerListener, year, month, day); 
      Calendar calendar = Calendar.getInstance(); 

      calendar.add(Calendar.DATE, 0); // Add 0 days to Calendar 
      Date newDate = calendar.getTime(); 
      datePickerDialog.getDatePicker().setMinDate(newDate.getTime()-(newDate.getTime()%(24*60*60*1000))); 
      return datePickerDialog; 
    } 
    return null; 
} 

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { 
    // the callback received when the user "sets" the Date in the 
    // DatePickerDialog 
    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { 

     textViewTime.setText(selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear); 
    } 
}; 
+0

和'calendar.add(Calendar .DATE,0);''和日曆日曆本身,如果它沒有被使用?Date newDate = new Date();就夠了。 – CoolMind

0

好了,所以我使用MaterialDatePicker圖書館和它太簡單設置最小日期就可以了。

DatePickerDialog mDatePickerDialog = DatePickerDialog.newInstance(listener, 
       mTime.year, mTime.month, mTime.monthDay); 
mDatePickerDialog.setYearRange(2016, 2036); 
Calendar c = Calendar.getInstance(); 
c.setTimeInMillis(System.currentTimeMillis() - 1000); 
mDatePickerDialog.setMinDate(c); 
相關問題