2017-06-19 30 views
0

我起訴在我的項目日期選取器,但我不希望將來的日期啓用選擇: -如何禁用日期選擇將來的日期在android系統

我還使用了應答中的鏈接,

Disable future dates in Android date picker

,但它不爲我工作, 我採用了android 2.2.3工作室,和我的API爲22

public void showTruitonDatePickerDialog(View v) { 
    DialogFragment newFragment = new DatePickerFragment(); 
    newFragment.show(getFragmentManager(), "datePicker"); 
} 

public class DatePickerFragment extends DialogFragment implements 
     DatePickerDialog.OnDateSetListener { 

    @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); 

     // Create a new instance of DatePickerDialog and return it 
     return new DatePickerDialog(getActivity(), this, year, month, day); 

    } 

    public void onDateSet(DatePicker view, int year, int month, int day) { 

     ddate.setText(day + "/" + (month + 1) + "/" + year); 





    } 

    } 

回答

2

我想你在代碼中添加了「maxDateRange」示例代碼,但沒有調用它。只需將上面給出的代碼替換爲:

public void showTruitonDatePickerDialog(View v) { 
DialogFragment newFragment = new DatePickerFragment(); 
newFragment.show(getFragmentManager(), "datePicker"); 
} 

public class DatePickerFragment extends DialogFragment implements 
    DatePickerDialog.OnDateSetListener { 

@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); 

    // Create a new instance of DatePickerDialog and return it 
    DatePickerDialog dialog = new DatePickerDialog(getActivity(), mDateSetListener, cyear, cmonth, cday); 
    dialog.getDatePicker().setMaxDate(new Date().getTime()); 
    return dialog; 


} 

public void onDateSet(DatePicker view, int year, int month, int day) { 

    ddate.setText(day + "/" + (month + 1) + "/" + year); 





} 

} 

希望它能起作用!

+0

感謝它的工作... – User8713

+0

不客氣。 –

相關問題