This article describes the basic usage of Android DatePicker and DatePickerDialog. Share it for your reference, as follows:
DatePicker is used to set the time and alarm clock of your phone
1. Add a button control in the configuration file, and then add an event to the button to enable it to open DatePicker
//The following is anonymous internal class method(new (){ @Override public void onClick(View v) { //The callback function onCreateDialog() method can be called through the following method, where the parameters are passed to the onCreateDialog() method //Be sure to use the onCreateDialog() method, because its return value is a Dialog object showDialog(DATE_DIALOG_ID); } });
2. After using the showDialog method, the callback function onCreateDialog() method will be called, and the time setter pops up in the form of a dialog box will be opened.
@Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: // mDateSetListener This parameter is used to tell Android to click the set callback function after setting the time return new DatePickerDialog(this, mDateSetListener, 2011, 10, 25); case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, 14, 12, true); } return null; }
3. According to the DatePickerDialog construction method, you need to add a "listener", that is, the method that the user will process after clicking the SET button.
// Some callback functions after DatePickerDialog is setpublic mDateSetListener = new () { //The following parameters are the time after the user has set it @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { (""); (new StringBuffer().append(year).append("-").append(monthOfYear).append("-").append(dayOfMonth)); } };
Expansion: Similarly, setting time is similar to this process
For more information about Android related content, please check out the topic of this site:Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.