Android DatePicker and Datepickerdialog basic usage examples _android

Source: Internet
Author: User

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.

Related Article

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.