2

这是我的日期选择器对话框代码。我想在我的日期选择器对话框中禁用未来日期选择。

任何人都可以建议如何?

@Override
protected Dialog onCreateDialog(int id)
{
    // TODO Auto-generated method stub
    switch(id)
    {
        case 1:
            Calendar cal=Calendar.getInstance();
            Toast.makeText(DateActivity.this, "- onCreateDialog -", Toast.LENGTH_LONG).show();
            return new DatePickerDialog(this,myDateSetListener,cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_YEAR));

        default:
            return null;
    }
}

private DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener()
{
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
    {
        // TODO Auto-generated method stub
        String date = "Year: " + String.valueOf(year) + "\n"
                    + "Month: " + String.valueOf(monthOfYear+1) + "\n"
                    + "Day: " + String.valueOf(dayOfMonth);

        Toast.makeText(DateActivity.this, date,Toast.LENGTH_LONG).show();
    }
};
4

3 回答 3

1

设置一个OnDateChangedListener到你的DateTimePicker,在哪里

onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)

检查日期是否在您允许的范围内,如果不在,请view.updateDate使用您的最大允许日期来电。

于 2013-05-07T05:35:53.840 回答
1

把这个方法放在onCreateDialog里面,解决了这个问题,未来的日期一定不是日期选择器dailog上的选择,

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        DatePickerDialog mDatePickerDialog = new DatePickerDialog(this, mDateSetListener, intYear, intMonth, intDay);
        mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis()); // Important as Future date not selection
        return mDatePickerDialog;
    }
    return null;
}
于 2014-03-11T05:37:58.320 回答
-1

yourdatepicker.getDatePicker().setMaxDate(System.currentTimeMillis());

于 2016-05-11T07:22:04.247 回答