The examples in this article describe the basics of Android DatePicker and Datepickerdialog usage. Share to everyone for your reference, specific as follows:
DatePicker used to set the phone's time and alarm clock.
1, add a button control in the configuration file, and then add an event to the button to enable it to open the DatePicker
The following is the method of anonymous inner class
Btn.setonclicklistener (New Button.onclicklistener () {
@Override public
void OnClick ( View v { //The callback function Oncreatedialog () method can be called by using the following method, where the parameter is passed to the Oncreatedialog () method
//Must be used Oncreatedialog () method, Because its return value is the DIALOG object
ShowDialog (date_dialog_id);
}
);
2. After using the ShowDialog method, the callback function Oncreatedialog () method is invoked, which opens a time setup in the form of a dialog box
@Override
protected Dialog oncreatedialog (int id) {
switch (ID) {case
date_dialog_id:
// Mdatesetlistener This parameter is used to tell Android after setting the time to click the set callback function to return to the
new Datepickerdialog (this,
Mdatesetlistener, (a
);
Case TIME_DIALOG_ID: Return to
new Timepickerdialog (This, Mtimesetlistener, A, true);
}
return null;
}
3, according to Datepickerdialog this construction method needs to add a "listener", that is, the user clicks the Set button will be processed after the method
Datepickerdialog after setting up some callback functions public
datepickerdialog.ondatesetlistener Mdatesetlistener = new Datepickerdialog.ondatesetlistener () {
//The following parameter is the time after the user has set up
@Override public
void Ondateset ( DatePicker view, int year, int monthofyear,
int dayofmonth) {
System.out.println (" Datepickerdialog.ondatesetlistener ");
Dateshowview.settext (New StringBuffer (). Append (year). Append ("-"). Append (Monthofyear). Append ("-"). Append ( DayOfMonth));
}
;
Extension: Similarly, the setting of time is similar to the process
For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls
I hope this article will help you with the Android program.