// 手动设置按钮
// mDialog是DatePickerDialog类型的;
DatePickerDialog mDialog = new DatePickerDialog(this, null, year, month, day);
mDialog.setButton(DialogInterface.BUTTON_POSITIVE, "完成",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 通过mDialog.getDatePicker()获得dialog上的DatePicker组件,然后可以获取日期信息
DatePicker datePicker = mDialog.getDatePicker();
int year = datePicker.getYear();
int month = datePicker.getMonth() + 1;
int day = datePicker.getDayOfMonth();
String month1 = String.valueOf(month);
String day1 = String.valueOf(day);
if (month < 10) {
month1 = "0" + month1;
}
if (day < 10) {
day1 = "0" + day1;
}
tv_birthday.setText(year + "-" + month1 + "-" + day1);
mDialog.dismiss();
}
});
// 取消按钮,如果不需要直接不设置即可
mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("BUTTON_NEGATIVE~~");
mDialog.dismiss();
}
});
mDialog.show();
关于日期选择器的一些小应用