Call of the time dialog box in Android development, Android dialog box

Source: Internet
Author: User

Call of the time dialog box in Android development, Android dialog box

In Android development, the time dialog box is often used. Generally, the date is selected in an EditText file. Next we will learn how to use the time dialog box.

1. First, set two controls in the corresponding XML file, one TextView and one EditText. Write operations on EditText in activity.

2. Declare the variable (year, month, day) and txtTime (EditText control, used to display the time) and initialize it accordingly.

TextView tvInTime; EditText txtInTime; // year, month, and day private int mYear; private int mMonth; private int mDay;

tvInTime =(TextView)  findViewById(R.id.tvInTime);txtInTime = (EditText) findViewById(R.id.txtInTime);

3. Set a click RESPONSE event for the time text box

// Set and click txtInTime for the listening event in the time text box. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// display date selection dialog box showDialog (DATE_DIALOG_ID );}});

4. initialize the Calendar Object and display the current system time.

// Initialize the Calendar Object final Calendar c = Calendar. getInstance (); mYear = c. get (Calendar. YEAR); // obtain the YEAR mMonth = c. get (Calendar. MONTH); // obtain the MONTH mDay = c. get (Calendar. DAY_OF_MONTH); // obtain the number of days // display the current system time updateDisplay ();

5. Customize the display time of the method.

// Use the OnDateSetListener listener to set the system time dialog box private DatePickerDialog. onDateSetListener mDateSetListener = new DatePickerDialog. onDateSetListener () {public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth) {mYear = year; // assign mMonth = monthOfYear to the year; // assign mDay = dayOfMonth to the month; // assign updateDisplay () to the day; // display the set date }}; /** custom method to display the system time */private void updateDisplay () {// display the set time txtInTime. setText (new StringBuilder (). append (mYear ). append ("-"). append (mMonth + 1 ). append ("-"). append (mDay ));}

Of course, some other controls are required in the XML file, such as buttons. You can set them and set the corresponding methods. Here is a brief introduction to the pop-up of the time dialog box and the methods you can choose freely.

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.