In Android development, you often use the time dialog box. The date is usually selected in a edittext. Now let's learn how to use the Time dialog box.
1. First, you set up two controls in the corresponding XML file, one TextView, and one edittext. Write the action on the EditText in the activity.
2. Declare the variable (month date) and Txttime (EditText control, which is used to display the time) and initialize it accordingly.
TextView Tvintime; EditText txtintime;//Date private int myear;private int mmonth;private int mday;
Tvintime = (TextView) Findviewbyid (r.id.tvintime); txtintime = (EditText) Findviewbyid (r.id.txtintime);
3. Set the click Response event for the time text box
Set the time text box to click Listen Event Txtintime.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {// Display Date Selection Dialog ShowDialog (date_dialog_id);}});
4. Initialize the Calendar object and display the current time of the system.
Initializes the calendar object to final calendars C = calendar.getinstance (); myear = C.get (calendar.year);//Get year Mmonth = C.get ( Calendar.month);//Gets the month Mday = C.get (calendar.day_of_month);//Gets the number of days//Displays the current system time updatedisplay ();
5. The custom method displays the time.
Setting the System Time dialog box with the Ondatesetlistener listener private datepickerdialog.ondatesetlistener Mdatesetlistener = new Datepickerdialog.ondatesetlistener () {public void Ondateset (DatePicker view, int year, int monthofyear,int dayofmonth) { Myear = year;//is assigned the year Mmonth = monthofyear;//is assigned the month Mday = dayofmonth;//is the talent value updatedisplay ();//Display Set Date}};/** custom method Display System time */ private void Updatedisplay () {//Displays the set time Txtintime.settext (new StringBuilder (). Append (Myear). Append ("-"). Append ( Mmonth + 1). Append ("-"). Append (Mday));}
Of course, in the XML file must have some other controls, such as buttons, these set themselves, and set the corresponding method on the line. Here is a brief introduction to the pop-up of the time dialog and the way you choose freely.
Call of the Time dialog box in Android development