从 DatePickerDialog 中删除年份 [英] Remove year from DatePickerDialog
本文介绍了从 DatePickerDialog 中删除年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Android 的基本 DatePickerDialog 教程,在此处找到 Android Date选择器
I'm using the basic DatePickerDialog tutorial by Android found here Android Date Picker
我已经开始工作了.但我只想能够从今年中选择一个日期.那么有没有办法从对话框中删除年份,以便只显示月份和日期?每次我尝试取出 Year int 时,它都会破坏代码.
and I've got it working. But I only want to be able to pick a date from this year. So is there a way to remove the year from the Dialog so that only the month and day are displayed? Every time I try and take the Year int out it breaks the code.
推荐答案
是的,有可能.DatePicker内置了三个NumberPicker控件,依次是年、月、日,先隐藏起来.
Yes it possible. DatePicker has the built-in three NumberPicker control, followed by the year, month, day, hide out first to.
final Calendar cal = Calendar.getInstance();
mDialog = new CustomerDatePickerDialog(getContext(), this,
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
mDialog.show();
DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView());
if (dp != null) {
((ViewGroup) dp.getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
}
CustomerDatePickerDialog
class CustomerDatePickerDialog extends DatePickerDialog {
public CustomerDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
super.onDateChanged(view, year, month, day);
mDialog.setTitle((month + 1) + "-" + day + "-");
}
}
日期选择器:
private DatePicker findDatePicker(ViewGroup group) {
if (group != null) {
for (int i = 0, j = group.getChildCount(); i < j; i++) {
View child = group.getChildAt(i);
if (child instanceof DatePicker) {
return (DatePicker) child;
} else if (child instanceof ViewGroup) {
DatePicker result = findDatePicker((ViewGroup) child);
if (result != null)
return result;
}
}
}
return null;
}
这篇关于从 DatePickerDialog 中删除年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文