Previous: http://www.bkjia.com/kf/201208/146229.html
This example describes how to use DatePickerDialog and TimePickerDialog in the time dialog box to obtain the time or date input.
DatePickerDialog and TimePickerDialog are both subclasses of AlertDialog. Therefore, they are used in the same way as Alert: Android ApiDemos example resolution (22): App-> Dialog
1. Define the variables for storing the date and time
[Java]
// Date and time
Private int mYear;
Private int mMonth;
Private int mDay;
Private int mHour;
Private int mMinute;
// Date and time
Private int mYear;
Private int mMonth;
Private int mDay;
Private int mHour;
Private int mMinute;
2. Define the ID for the date and time dialog box
[Java]
Static final int TIME_DIALOG_ID = 0;
Static final int DATE_DIALOG_ID = 1;
Static final int TIME_DIALOG_ID = 0;
Static final int DATE_DIALOG_ID = 1;
3. initialize the dialog box in the onCreateDialog event of the Activity
[Java]
Switch (id ){
Case TIME_DIALOG_ID:
Return new TimePickerDialog (this,
MTimeSetListener, mHour, mMinute, false );
Case DATE_DIALOG_ID:
Return new DatePickerDialog (this,
MDateSetListener,
MYear, mMonth, mDay );
}
Switch (id ){
Case TIME_DIALOG_ID:
Return new TimePickerDialog (this,
MTimeSetListener, mHour, mMinute, false );
Case DATE_DIALOG_ID:
Return new DatePickerDialog (this,
MDateSetListener,
MYear, mMonth, mDay );
}
4. the time or date dialog box can be displayed multiple times in the application. In the onCreateDialog initialization dialog box, You can reset the value of the dialog box in onPrepareDialog each time you re-display the dialog box, in this example, the time and date values displayed on the current screen are displayed in the dialog box.
[Java]
Switch (id ){
Case TIME_DIALOG_ID:
(TimePickerDialog) dialog)
. UpdateTime (mHour, mMinute );
Break;
Case DATE_DIALOG_ID:
(DatePickerDialog) dialog)
. UpdateDate (mYear, mMonth, mDay );
Break;
}
Switch (id ){
Case TIME_DIALOG_ID:
(TimePickerDialog) dialog)
. UpdateTime (mHour, mMinute );
Break;
Case DATE_DIALOG_ID:
(DatePickerDialog) dialog)
. UpdateDate (mYear, mMonth, mDay );
Break;
}
5. constructor Definition
In this example, the constructor used by TimePickerDialog is defined as follows:
Public TimePickerDialog (Context context,
TimePickerDialog. OnTimeSetListener callBack,
Int hourOfDay,
Int minute, boolean is24HourView)
HourOfDay hour
Int minute minutes
Is is24HourView in 24-hour format?
Callback: Specifies the callback function of the notification application after the user selects a time. Its type is TimePickerDialog. OnTimeSetListener, which can be used to obtain the time in the current time dialog box:
[Java]
Private TimePickerDialog. OnTimeSetListener mTimeSetListener =
New TimePickerDialog. OnTimeSetListener (){
Public void onTimeSet (TimePicker view,
Int hourOfDay, int minute ){
MHour = hourOfDay;
MMinute = minute;
UpdateDisplay ();
}
};
Private TimePickerDialog. OnTimeSetListener mTimeSetListener =
New TimePickerDialog. OnTimeSetListener (){
Public void onTimeSet (TimePicker view,
Int hourOfDay, int minute ){
MHour = hourOfDay;
MMinute = minute;
UpdateDisplay ();
}
};
DatePickerDialog the constructor used in this example is as follows:
[Java]
Public DatePickerDialog (Context context,
DatePickerDialog. OnDateSetListener callBack,
Int year,
Int monthOfYear,
Int dayOfMonth)
Public DatePickerDialog (Context context,
DatePickerDialog. OnDateSetListener callBack,
Int year,
Int monthOfYear,
Int dayOfMonth) year
MonthOfYear month
DayOfMonth
After the callBack user selects a date, it notifies the application of the callBack function, whose type is TimePickerDialog. OnTimeSetListener, which can be used to obtain the date in the current time dialog box:
[Java]
Private TimePickerDialog. OnTimeSetListener mTimeSetListener =
New TimePickerDialog. OnTimeSetListener (){
Public void onTimeSet (TimePicker view, int hourOfDay, int minute ){
MHour = hourOfDay;
MMinute = minute;
UpdateDisplay ();
}
};
Private TimePickerDialog. OnTimeSetListener mTimeSetListener =
New TimePickerDialog. OnTimeSetListener (){
Public void onTimeSet (TimePicker view, int hourOfDay, int minute ){
MHour = hourOfDay;
MMinute = minute;
UpdateDisplay ();
}
};
Author: mapdigit