摘要:Android - 客製化只有年月的DatePickerDialog
public static DatePickerDialog createMonthYearDatePicker(Context ctx,int year,int month,int day,DatePickerDialog.OnDateSetListener listener) {
DatePickerDialog dpd = new DatePickerDialog(ctx, listener,year, month, day) {
//複寫onStop,讓取消時 ,不呼叫 onDateSet方法
@Override
protected void onStop() {
//super.onStop();
}
};
dpd.setButton(DialogInterface.BUTTON_NEGATIVE, ctx.getString(R.string.btn_cancle),new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dpd.setTitle("");
try {
Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
for (Field datePickerField : datePickerFields) {
if ("mDayPicker".equals(datePickerField.getName()) || "mDaySpinner".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
} catch (Exception ex) {
}
return dpd;
};