如何禁用日期选择将来的日期在android系统

我起诉在我的项目日期选取器,但我不希望将来的日期启用选择: -如何禁用日期选择将来的日期在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);

}

}

回答:

我想你在代码中添加了“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);

}

}

希望它能起作用!

以上是 如何禁用日期选择将来的日期在android系统 的全部内容, 来源链接: utcz.com/qa/266360.html

回到顶部