用户选择日期并重新打开datepicker对话框后,它设置为当前日期...而不是所选日期...如何在用户重新打开datepicker对话框后设置为选择日期到datepicker,这是我的代码
@Override
public void onClick(View v) {
Dialog.Builder builder = null;
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR)+10;
c.set(year, c.get(Calendar.MONTH), c.get(Calendar.DATE));
builder = new DatePickerDialog.Builder(R.style.CustomStyle_DatePicker) {
@Override
public void onPositiveActionClicked(DialogFragment fragment) {
DatePickerDialog dialog = (DatePickerDialog) fragment.getDialog();
String date = dialog.getFormattedDate(SimpleDateFormat.getDateInstance());
selected_date.setText(date);
super.onPositiveActionClicked(fragment);
}
@Override
public void onNegativeActionClicked(DialogFragment fragment) {
super.onNegativeActionClicked(fragment);
}
}.dateRange(System.currentTimeMillis() - 1000, c.getTimeInMillis());
builder.positiveAction("OK")
.negativeAction("CANCEL");
DialogFragment fragment = DialogFragment.newInstance(builder);;
fragment.show(getFragmentManager(), null);
}
我一直在寻找,但我无法让它工作,有什么帮助吗?
答案 0 :(得分:3)
全球宣布:
Calendar selectedDate = Calendar.getInstance();
然后将日期设置为构建器,并在用户选择日期为selectedDate时保存,如下所示。这将设置当前日期,直到用户选择日期。
@Override
public void onClick(View v) {
Dialog.Builder builder = null;
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR)+10;
c.set(year, c.get(Calendar.MONTH), c.get(Calendar.DATE));
builder = new DatePickerDialog.Builder(R.style.CustomStyle_DatePicker) {
@Override
public void onPositiveActionClicked(DialogFragment fragment) {
DatePickerDialog dialog = (DatePickerDialog) fragment.getDialog();
String date = dialog.getFormattedDate(SimpleDateFormat.getDateInstance());
selected_date.setText(date);
selectedDate.set(dialog.getYear(),dialog.getMonth(), dialog.getDay());
super.onPositiveActionClicked(fragment);
}
@Override
public void onNegativeActionClicked(DialogFragment fragment) {
super.onNegativeActionClicked(fragment);
}
}.dateRange(System.currentTimeMillis() - 1000, c.getTimeInMillis()).date(selectedDate.get(Calendar.DATE),
selectedDate.get(Calendar.MONTH)
, selectedDate.get(Calendar.YEAR));
builder.positiveAction("OK")
.negativeAction("CANCEL");
DialogFragment fragment = DialogFragment.newInstance(builder);
fragment.show(getFragmentManager(), null);
}
答案 1 :(得分:0)
DatePickerDialog datePickerDialog ;
private void initialiseDatePicker(){
final Calendar c = Calendar.getInstance();
int mYear;
int mMonth;
int mDay;
//this is previous set date
String date = dobButton.getText().toString().trim();
//this is previous set date
if (date != null && date.length() > 0) {
//if you have previous set date
String data[] = date.split("-");
mYear = Integer.parseInt(data[2]);
mMonth = Integer.parseInt(data[1]);
mMonth = mMonth - 1;
mDay = Integer.parseInt(data[0]);
} else {
//if you dont have previous set date then display current date
mYear = c.get(Calendar.YEAR);
mYear = mYear;
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
int mYear1 = c.get(Calendar.YEAR);
int mMonth1 = c.get(Calendar.MONTH);
int mDay1 = c.get(Calendar.DAY_OF_MONTH);
Calendar maxDate = Calendar.getInstance();
maxDate.set(mYear1, mMonth1, mDay1);
Calendar minDate = Calendar.getInstance();
minDate.set(mYear1 - 150, mMonth1, mDay1);
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String dateOfBirth1 = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
dobButton.setText(dateOfBirth1);
}
}, mYear, mMonth, mDay);
final DatePicker datePicker = datePickerDialog.getDatePicker();
datePicker.setMinDate(minDate.getTimeInMillis());
datePicker.setMaxDate(maxDate.getTimeInMillis());
datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
datePicker.clearFocus();
int year = datePicker.getYear();
int monthOfYear=datePicker.getMonth();
int dayOfMonth = datePicker.getDayOfMonth();
String dateOfBirth1 = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
dobButton.setText(dateOfBirth1);
}
});
datePickerDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
dobButton.setText(dateOfBirth);
}
});
}