我有一个日期选择器。 datepicker在除android 4.3之外的所有设备上都能正常工作。当我点击datepicker的取消按钮时,设置日期而不是cacelling本身。这是我的代码。
DatePickerDialog datepicker = new DatePickerDialog(c,
new OnDateSetListener() {
@Override
public void onDateSet(android.widget.DatePicker view,
int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
tv.setText(CommonDateFunction.format(newDate.getTime(),
CommonDateFunction.FORMAT_DD_MMM_YYYY));
}
}, newCalendar.get(Calendar.YEAR),
newCalendar.get(Calendar.MONTH),
newCalendar.get(Calendar.DAY_OF_MONTH));
datepicker.show();
答案 0 :(得分:0)
设置日期选择器的按钮以避免此错误。这是一个例子。
Calendar newCalendar = Calendar.getInstance();
final DatePickerDialog datepicker = new DatePickerDialog(c,null, newCalendar.get(Calendar.YEAR),
newCalendar.get(Calendar.MONTH),
newCalendar.get(Calendar.DAY_OF_MONTH));
datepicker.setCancelable(true);
datepicker.setCanceledOnTouchOutside(true);
datepicker.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Calendar newDate = Calendar.getInstance();
int dayOfMonth = datepicker.getDatePicker().getDayOfMonth();
int monthOfYear = datepicker.getDatePicker().getMonth() ;
int year = datepicker.getDatePicker().getYear();
newDate.set(year, monthOfYear, dayOfMonth);
tv.setText(CommonDateFunction.format(newDate.getTime(),
CommonDateFunction.FORMAT_DD_MMM_YYYY));
}
});
datepicker.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
// Log.d("Picker", "Cancel!");
}
});
datepicker.show();