如何在 DatePicker 中设置当前日期? [英] How do I set the current date in a DatePicker?
问题描述
我需要能够在我的申请中选择日期和时间应该通过使用对话框或开始一个新的意图来完成.
I need to be able to choose date and time in my application It should either be done by using a dialog, or starting a new intent.
我现在的做法是,我开始了一个新的意图,因为我需要设置日期和时间.
The way I have done it now, is that I have started a new intent, since I need to set both date and time.
我的问题是我需要在 Datepicker
上设置当前日期(这可以通过 Timepicker
使用 setCurrentHour
和 >setCurrentMinute
) onCreate,并将其设置为下边界.
My problem is that I need to set the current date on the Datepicker
(This is possible with the Timepicker
using setCurrentHour
and setCurrentMinute
) onCreate, and set that as a lower boundary.
我在 Android API 中找不到任何用于使用 DatePicker
执行此操作的函数.
I can't find any functions in the Android API for doing this with the DatePicker
.
有人有解决这个问题的建议吗?
Anyone have a suggestion to solve this problem?
提前致谢!
重要提示:DatePicker
和 TimePicker
必须在同一个对话框/意图中
Important: The DatePicker
and TimePicker
must be in the same dialog/intent
推荐答案
直接编码
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(this, mDateSetListener, mYear, mMonth, mDay);
dialog.show();
这篇关于如何在 DatePicker 中设置当前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!