所以我已经阅读了其他帖子,但仍然无法让它正常工作。我想禁用某人从我的datepickerfragment中选择未来日期的功能。任何帮助将不胜感激,谢谢!
[更新] 2015年4月26日 - 解决了所有评论或回答的人 - 他们的解决方案有效。
调用datepicker的函数:
mSearchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.show(getSupportFragmentManager(), "datepicker");
}
});
DatePickerFragment代码:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public EditText editText;
DatePicker dpResult;
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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Intent goSearchSelectedDate = new Intent(getActivity(), SearchByDate.class);
goSearchSelectedDate.putExtra("day", String.valueOf(day));
goSearchSelectedDate.putExtra("month", String.valueOf(month));
goSearchSelectedDate.putExtra("year", String.valueOf(year));
Log.d("month", String.valueOf(year));
startActivity(goSearchSelectedDate);
}
}
答案 0 :(得分:6)
您只需在DatePickerDialog
datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());
答案 1 :(得分:2)
我采用了everyones解决方案并更新了我的代码。见下文。我为每个人都注释了旧代码以查看差异。谢谢大家的帮助!
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public EditText editText;
DatePicker dpResult;
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);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
//return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Intent goSearchSelectedDate = new Intent(getActivity(), Searched_Game_ListView.class);
Intent goSearchSelectedDate = new Intent(getActivity(), SearchByDate.class);
goSearchSelectedDate.putExtra("day", String.valueOf(day));
goSearchSelectedDate.putExtra("month", String.valueOf(month));
goSearchSelectedDate.putExtra("year", String.valueOf(year));
Log.d("month", String.valueOf(year));
startActivity(goSearchSelectedDate);
}
}
我叫它:
mSearchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.show(getSupportFragmentManager(), "datepicker");
}
});