2016-07-13 90 views
0

我在Android的新的,請幫幫我,我該怎麼禁用過去的日期選擇日期,我不知道如何執行這個東西,Android的我怎麼能禁止過去的日期中的DatePicker

+2

的可能的複製[如何禁用Android的日期選取過去的日期?(http://stackoverflow.com/questions/23762231/how-to-disable -past-日期功能於Android的日期選擇器) –

回答

3

你可以做

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

將今天的日期設置爲最短日期和所有過去的日期都被禁用。

日期選擇是的DatePicker的對象,如果你正在使用DatePickerDialog的對象,你可以做

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

我使用這個類日期選擇器。有了這個,你可以設定最低和最高日

public class Datepicker extends DialogFragment 
    implements DatePickerDialog.OnDateSetListener { 
TextView textView; 
Context context; 
private Boolean isMinDate; 
private DatePickerDialog datepic; 

public Datepicker() { 
} 

public Datepicker(TextView textView, Context context, Boolean isMinDate) { 
    this.context = context; 
    this.textView = textView; 
    this.isMinDate = isMinDate; 
} 

public Datepicker(TextView textView, Context context) { 
    this.context = context; 
    this.textView = textView; 

} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Use the current date as the default date in the picker 
    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); 
    datepic = new DatePickerDialog(getActivity(), this, year, month, day); 
    datepic.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    if (isMinDate != null) { 
     if (isMinDate) { 
      setMinimumDate(c); 
     } else { 
      setMaximumDate(c); 
     } 
    } 
    // Create a new instance of DatePickerDialog and return it 
    return datepic; 
} 

@Override 
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) { 

    int month = monthOfYear + 1; 
    textView.setText(year + "-" + month + "-" + dayOfMonth); 
} 

public void setMinimumDate(Calendar mintime) { 
    datepic.getDatePicker().setMinDate(mintime.getTimeInMillis()); 
} 

public void setMaximumDate(Calendar maxTime) { 
    datepic.getDatePicker().setMaxDate(new Date().getTime()); 
} 
相關問題