如何在android对话框API 5.0中限制未来的日期

时间:2015-04-01 05:28:27

标签: android

在Android 5.0版日期选择器对话框中如果我们使用以下代码限制未来日期。

public class DatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {

TextView dateText;

public DatePicker(final TextView dateText) {
    this.dateText = dateText;
}

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    int year = 0, month = 0, day = 0;
    final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog datepicker = new DatePickerDialog(getActivity(), this, year, month, day);
datepicker.getDatePicker().setMaxDate(System.currentTimeMillis());
    return datepicker;
}


@Override
public void onDateSet(final android.widget.DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
    final String dateFormat = "yyyy/MM/dd";
    final SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
        date = form.parse(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
    } catch (final ParseException e) {
        Log.d("DatePicker", e.getMessage());
    }
    final SimpleDateFormat postFormater = new SimpleDateFormat(dateFormat);
    final String formattedDate = postFormater.format(date);
    dateText.setText(formattedDate);
}

}

未来日期的文字颜色变为灰色,但能够选择日期。如何在android 5.0中限制将来日期的日期选择。

0 个答案:

没有答案