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.
我现在的方式是,我已经开始了新的意图,因为我需要设置日期和时间。
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.
我的问题是我需要在Datepicker上设置当前日期(这可以通过使用setCurrentHour和setCurrentMinute的Timepicker)onCreate,并将其设置为下边界。
I can't find any functions in the Android API for doing this with the DatePicker
.
我在Android API中找不到使用DatePicker执行此操作的任何功能。
Anyone have a suggestion to solve this problem?
有人建议解决这个问题吗?
Thanks in advance!
提前致谢!
Edit: Important: The DatePicker
and TimePicker
must be in the same dialog/intent
编辑:重要:DatePicker和TimePicker必须位于相同的对话框/意图中
6 个解决方案
#1
57
Straight to code
直接编码
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();
#2
18
Try this:
尝试这个:
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into datepicker
datepicker.init(year, month, day, null);
#3
8
If you are simply looking for the equivalent of setCurrentHour()
and setCurrentMinute()
on TimePicker
, but on DatePicker
- see updateDate (int year, int month, int dayOfMonth) on DatePicker
.
如果您只是在TimePicker上查找等效的setCurrentHour()和setCurrentMinute(),但是在DatePicker上 - 请参阅DatePicker上的updateDate(int year,int month,int dayOfMonth)。
DatePicker mDatePicker = (DatePicker) findViewById(R.id.datePicker);
mDatePicker.updateDate(1994, 6, 12);
// Sets date displayed on DatePicker to 12th June 1994
#4
1
You can query the current date through the Calendar class.
您可以通过Calendar类查询当前日期。
Calendar now = Calendar.getInstance();
now.get(Calendar.WHATDOYOUNEED);
Feed it's get()
method with different parameters to customize your query.
使用不同的参数提供它的get()方法来自定义查询。
#5
0
The tutorial for the DatePicker seems to do exactly this?
DatePicker的教程似乎正是这样做的?
http://developer.android.com/resources/tutorials/views/hello-datepicker.html
http://developer.android.com/resources/tutorials/views/hello-datepicker.html
Refer to:
参考:
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date (this method is below)
updateDisplay();
#1
57
Straight to code
直接编码
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();
#2
18
Try this:
尝试这个:
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into datepicker
datepicker.init(year, month, day, null);
#3
8
If you are simply looking for the equivalent of setCurrentHour()
and setCurrentMinute()
on TimePicker
, but on DatePicker
- see updateDate (int year, int month, int dayOfMonth) on DatePicker
.
如果您只是在TimePicker上查找等效的setCurrentHour()和setCurrentMinute(),但是在DatePicker上 - 请参阅DatePicker上的updateDate(int year,int month,int dayOfMonth)。
DatePicker mDatePicker = (DatePicker) findViewById(R.id.datePicker);
mDatePicker.updateDate(1994, 6, 12);
// Sets date displayed on DatePicker to 12th June 1994
#4
1
You can query the current date through the Calendar class.
您可以通过Calendar类查询当前日期。
Calendar now = Calendar.getInstance();
now.get(Calendar.WHATDOYOUNEED);
Feed it's get()
method with different parameters to customize your query.
使用不同的参数提供它的get()方法来自定义查询。
#5
0
The tutorial for the DatePicker seems to do exactly this?
DatePicker的教程似乎正是这样做的?
http://developer.android.com/resources/tutorials/views/hello-datepicker.html
http://developer.android.com/resources/tutorials/views/hello-datepicker.html
Refer to:
参考:
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date (this method is below)
updateDisplay();