Datepicker可以保存1970年,无论选择并保存了正常日期。
如果有人有什么线索可以解决该问题,我将不胜感激。
该程序要求我写更多的文字,尽管我不知道该怎么写,但是我需要写点东西,这样它实际上可以让我发布它,因此我为不需要的文字表示歉意。
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
Activity act;
TextView showDate;
long datelimit = 0;
long setDate = 0;
boolean checkToDate = false, isNoPast = false;
String devicePattern;
public DatePickerFragment(Activity act, TextView lblDate1, String setDate) {
this.act = act;
showDate = lblDate1;
this.setDate = Long.valueOf(setDate);
SimpleDateFormat deviceDateFormat = (SimpleDateFormat) android.text.format.DateFormat.getDateFormat(act.getApplicationContext());
devicePattern = deviceDateFormat.toLocalizedPattern();
}
public DatePickerFragment(Activity act, TextView lblDate1, String startFrom, String setDate) {
this.act = act;
showDate = lblDate1;
this.datelimit = Long.valueOf(startFrom);
this.setDate = Long.valueOf(setDate);
SimpleDateFormat deviceDateFormat = (SimpleDateFormat) android.text.format.DateFormat.getDateFormat(act.getApplicationContext());
devicePattern = deviceDateFormat.toLocalizedPattern();
}
public DatePickerFragment() {
}
public DatePickerFragment(Activity act, EditText lblDate1, String startFrom, boolean checkToDate, String setDate) {
this.act = act;
showDate = lblDate1;
this.datelimit = Long.valueOf(startFrom);
this.checkToDate = checkToDate;
this.setDate = Long.valueOf(setDate);
SimpleDateFormat deviceDateFormat = new SimpleDateFormat(AppConstants.APP_DATE_FORMAT, Locale.getDefault());
devicePattern = deviceDateFormat.toLocalizedPattern();
}
public DatePickerFragment(Activity act, EditText lblDate1, String startFrom, boolean checkToDate, String setDate, boolean isNoPast) {
this.act = act;
showDate = lblDate1;
this.datelimit = Long.valueOf(startFrom);
this.checkToDate = checkToDate;
this.setDate = Long.valueOf(setDate);
this.isNoPast = isNoPast;
SimpleDateFormat deviceDateFormat = new SimpleDateFormat(AppConstants.APP_DATE_FORMAT, Locale.getDefault());
devicePattern = deviceDateFormat.toLocalizedPattern();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
if (setDate > 0)
c.setTimeInMillis(setDate);
else if (!checkToDate && datelimit > 0)
c.setTimeInMillis(datelimit);
final int myear = c.get(Calendar.YEAR);
final int mmonth = c.get(Calendar.MONTH);
final int mday = c.get(Calendar.DAY_OF_MONTH);
final Calendar calendar = Calendar.getInstance();
if (datelimit != 0)
calendar.setTimeInMillis(datelimit);
final int minYear = calendar.get(Calendar.YEAR);
final int minMonth = calendar.get(Calendar.MONTH);
final int minDay = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, myear, mmonth, mday);
if (isNoPast) {
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
}
if (!checkToDate && datelimit > 0) {
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
} else if (checkToDate && datelimit > 0) {
datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
}
datePickerDialog.getDatePicker().init(myear, mmonth, mday, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
if (checkToDate && datelimit != 0) {
if (newDate.after(calendar)) {
view.init(minYear, minMonth, minDay, this);
}
} else if (!checkToDate && datelimit != 0) {
/*Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(datelimit);
Long date2 = c1.getTimeInMillis();
view.init(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH), this);*/
if (newDate.before(calendar)) {
view.init(minYear, minMonth, minDay, this);
}
}
}
});
/*if (checkToDate && datelimit != 0)
datePickerDialog.getDatePicker().setMaxDate(datelimit);
else if (!checkToDate && datelimit != 0)
datePickerDialog.getDatePicker().setMinDate(datelimit);*/
datePickerDialog.setCancelable(true);
datePickerDialog.setCanceledOnTouchOutside(true);
return datePickerDialog;
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
Long date1 = c.getTimeInMillis();
/*Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(datelimit);
Long date2 = c1.getTimeInMillis();
if (datelimit > 0) {
if (!checkToDate) {
if (date1 < date2) {
Toast.makeText(act, "To date should not be less than From date ", Toast.LENGTH_SHORT).show();
*//*showDate.setTag(c1.getTimeInMillis());
//int month = monthOfYear + 1;
Date selectedDate = new Date(c1.getTimeInMillis());
String finalDate = new SimpleDateFormat(devicePattern, Locale.getDefault()).format(selectedDate);
showDate.setText(finalDate);*//*
} else {
showDate.setTag(c.getTimeInMillis());
//int month = monthOfYear + 1;
Date selectedDate = new Date(c.getTimeInMillis());
String finalDate = new SimpleDateFormat(devicePattern, Locale.getDefault()).format(selectedDate);
showDate.setText(finalDate);
}
} else {
showDate.setTag(c.getTimeInMillis());
//int month = monthOfYear + 1;
Date selectedDate = new Date(c.getTimeInMillis());
String finalDate = new SimpleDateFormat(devicePattern, Locale.getDefault()).format(selectedDate);
showDate.setText(finalDate);
}
} else {
showDate.setTag(c.getTimeInMillis());
//int month = monthOfYear + 1;
Date selectedDate = new Date(c.getTimeInMillis());
String finalDate = new SimpleDateFormat(devicePattern, Locale.getDefault()).format(selectedDate);
showDate.setText(finalDate);
}*/
showDate.setTag(c.getTimeInMillis());
//int month = monthOfYear + 1;
Date selectedDate = new Date(c.getTimeInMillis());
String finalDate = new SimpleDateFormat(devicePattern, Locale.getDefault()).format(selectedDate);
showDate.setText(finalDate);
//showDate.setText(dayOfMonth + "/" + month + "/" + year);
//showDate.setText(dayOfMonth + "/" + month + "/" + year+" "+CommonMembers.getDateTime(2));
}
@Override
public void onCancel(DialogInterface dialog) {
if (setDate > 0) {
showDate.setTag(setDate);
Date selectedDate = new Date(setDate);
String finalDate = new SimpleDateFormat(devicePattern, Locale.getDefault()).format(selectedDate);
showDate.setText(finalDate);
} else {
showDate.setTag(null);
showDate.setText("");
}
super.onCancel(dialog);
}
}
答案 0 :(得分:0)
对话框片段的hmm必须使用no-arg构造函数,并将bundle与setArguments一起使用,以传递并使用onCreate的bundle获取这些值。
这是创建实例的方式:
DatePickerFragment fragment = DatePickerFragment.getInstance(arg1, arg2).
public static DatePickerFragment getInstance(String startFrom, String setDate) {
DatePickerFragment fragment = new DatePickerFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putString("startFrom", startFrom);
args.putString("setDate", setDate);
fragment.setArguments(args);
return fragment;
}
答案 1 :(得分:0)
问题是日期格式,我为此丢了3天:(