Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

android - 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.

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.

I can't find any functions in the Android API for doing this with the 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...