Android DatePickerDialog设置minDate和maxDate时出错
我正在使用Android DatePickerDialog设置minDate和maxDate时出错,android,android-fragments,datepicker,Android,Android Fragments,Datepicker,我正在使用DatePickerDialog设置最小和最大日期,但没有成功。我如何设置这些 我的密码是 public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{ @Override public Dialog onCreateDialog(Bundle savedInstanceState){ final
DatePickerDialog
设置最小和最大日期,但没有成功。我如何设置这些
我的密码是
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);
/*
add(int field, int value)
Adds the given amount to a Calendar field.
*/
// Add 3 days to Calendar
calendar.add(Calendar.DATE, 3);
/*
getTimeInMillis()
Returns the time represented by this Calendar,
recomputing the time from its fields if necessary.
getDatePicker()
Gets the DatePicker contained in this dialog.
setMinDate(long minDate)
Sets the minimal date supported by this NumberPicker
in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
setMaxDate(long maxDate)
Sets the maximal date supported by this DatePicker in milliseconds
since January 1, 1970 00:00:00 in getDefault() time zone.
*/
// Set the Calendar new date as maximum date of date picker
dpd.getDatePicker().setMaxDate(calendar.getTimeInMillis());
// Subtract 6 days from Calendar updated date
calendar.add(Calendar.DATE, -6);
// Set the Calendar new date as minimum date of date picker
dpd.getDatePicker().setMinDate(calendar.getTimeInMillis());
// So, now date picker selectable date range is 7 days only
// Return the DatePickerDialog
return dpd;
}
public void onDateSet(DatePicker view, int year, int month, int day){
// Do something with the chosen date
// TextView tv = (TextView) getActivity().findViewById(R.id.tv);
// Create a Date variable/object with user chosen date
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, month, day, 0, 0, 0);
Date chosenDate = cal.getTime();
// Format the date using style and locale
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
String formattedDate = df.format(chosenDate);
// Display the chosen date to app interface
// tv.setText(formattedDate);
}
@Override
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Do something with the chosen date
// TextView tv = (TextView) getActivity().findViewById(R.id.tv);
// Create a Date variable/object with user chosen date
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, monthOfYear, dayOfMonth, 0, 0, 0);
Date chosenDate = cal.getTime();
// Format the date using style and locale
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
String formattedDate = df.format(chosenDate);
// Display the chosen date to app interface
// tv.setText(formattedDate);
}
}
我在click view下调用这个类。从DatePickerFragment中删除重复的方法
onDateSet(DatePicker,int,int,int)
,如下所示
@Override
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Do something with the chosen date
// TextView tv = (TextView) getActivity().findViewById(R.id.tv);
// Create a Date variable/object with user chosen date
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, monthOfYear, dayOfMonth, 0, 0, 0);
Date chosenDate = cal.getTime();
// Format the date using style and locale
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
String formattedDate = df.format(chosenDate);
// Display the chosen date to app interface
// tv.setText(formattedDate);
}
我在DatePickerFragment中看到两个onDateSet方法。第二个是。如果没有帮助,请将代码发布到调用DatePickerFragment以显示的位置,并且您应该在第一个方法上方添加@Override。答案是什么?是否有帮助?