在使用此代码段的片段类中:
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, mYear, mMonth, mDay);
dialog.setButton(DatePickerDialog.BUTTON_POSITIVE, "OK", dialog);
dialog.setButton(DatePickerDialog.BUTTON_NEGATIVE, "ANNULLA", (DialogInterface.OnClickListener)null);
dialog.show();
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
//Do whatever you want
Log.d("DATEPICKER","La data presa è "+selectedDay+selectedMonth+selectedYear);
}
};
不幸的是,我点击了POSITIVE和NEGATIVE按钮的日期。当然,我只需要使用正面按钮来设置日期(但是,我在两个日志中都看到条目)。
答案 0 :(得分:0)
如果查看 DatePickerDialog 的源代码(在API 16上测试),您会注意到总是在 onStop()中调用 onDateSet() 方法。 我重写了默认的正按钮行为,并实现了正按钮监听器而不是 onDateSet()
private class ReportDatePickerDialog extends DialogFragment implements android.content.DialogInterface.OnClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(), null, initialYear, initialMonth, initialDay);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Ok", ReportDatePickerDialog.this);
return dialog;
}
@Override
public void onClick(DialogInterface dialog, int which) {
((DatePickerDialog)getDialog()).onClick(dialog, which);
if (which != DialogInterface.BUTTON_POSITIVE) {
return;
}
DatePicker datePicker = ((DatePickerDialog)getDialog()).getDatePicker();
datePicker.getDayOfMonth();
datePicker.getMonth();
datePicker.getYear();
}
}
我想最终在这种情况下创建自定义对话框窗口会更好。
答案 1 :(得分:-1)
这是新Android Lollipop日历应用的源代码链接
您可能还必须添加以下依赖项。
https://android.googlesource.com/platform/frameworks/ex/
https://android.googlesource.com/platform/frameworks/opt/colorpicker/
https://android.googlesource.com/platform/frameworks/opt/calendar/
https://android.googlesource.com/platform/frameworks/opt/datetimepicker/
https://android.googlesource.com/platform/frameworks/opt/timezonepicker/
https://android.googlesource.com/platform/packages/apps/Calendar.git/+/android-5.1.0_r3
您可以克隆回购。例如git clone https://android.googlesource.com/platform/frameworks/ex。但我担心的是,您可能还需要其他依赖项。但是,你必须先尝试。