Call of the Time dialog box in Android development

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.