本文介绍了Android DatePicker更改为只有月和年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DatePickerDialog,我只想查看月和年。如何更改此代码?
I have a DatePickerDialog and I want view only Month and Year. How can I change this code?
public void chooseDate2(View v) {
new DatePickerDialog(
act.this,
d1,
dateAndTime1.get(Calendar.YEAR) + 2,
dateAndTime1.get(Calendar.MONTH),
dateAndTime1.get(Calendar.DAY_OF_MONTH)
).show();
}
private void updateLabel2() {
scadenza.setText(fmtDateAndTime.format(dateAndTime1.getTime()));
}
DatePickerDialog.OnDateSetListener d1=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateAndTime1.set(Calendar.YEAR, year);
dateAndTime1.set(Calendar.MONTH, monthOfYear);
dateAndTime1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel2();
}
};
谢谢
推荐答案
尝试以下代码。它将显示一个 DatePicker
,只有年和月(不含日)
Try the following code. It will show a DatePicker
with only the year and month (without day)
private DatePickerDialog createDialogWithoutDateField() {
DatePickerDialog dpd = new DatePickerDialog(this, null, 2014, 1, 24);
try {
java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();
for (java.lang.reflect.Field datePickerField : datePickerFields) {
Log.i("test", datePickerField.getName());
if ("mDaySpinner".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
}
catch (Exception ex) {
}
return dpd;
}
此方法返回一个日期选择器对话框。因此,在您的按钮的 onClick
方法中,添加以下代码以显示对话框。
This method returns a date picker dialog. So , in your button's onClick
method add the following code to display your dialog.
createDialogWithoutDateField().show();
这篇关于Android DatePicker更改为只有月和年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!