![](/img/trans.png)
[英]Update date in Android DatePickerDialog with Today button without closing dialog
[英]Android: datepickerdialog cancel dialog without getting the date
在片段类中,我使用以下片段:
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);
}
};
不幸的是,我要同时单击“正”和“负”按钮。 当然,我只需要使用肯定按钮来显示日期(但是,我在两者的日志中都看到了条目)。
如果查看DatePickerDialog的源代码(在API 16上测试),您会注意到onDateSet()始终在onStop()方法中被调用。 我重写了默认的肯定按钮行为,并实现了肯定按钮侦听器,而不是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();
}
}
我想最终最好在这种情况下创建自定义对话框窗口。
这是新的Android Lollipop Calendar应用程序的源代码链接。
您可能还必须添加以下依赖项。
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 。 但是我担心您可能还需要其他依赖项。 但是,您必须先尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.