Android uses DatePicker and timepicker to display the current date and time

Source: Internet
Author: User
<span id="Label3"></p><p><p>Course Content<br>1. Introduction of two DatePicker and timepicker to implement dynamic input date and event functions<br>2. Introduction of Datepickerdialog and Timepickerdialog in the coming year dialog box for dynamically entering dates and events<br>3. Two sets of listeners for detection date and time changes are included:<br>Ondatechangedlistener and Ontimechangedlistener<br>Ondatesetlistener and Ontimesetlistener<br>When the user changes the year, month, and day in the datepicker, the Ondatechange () event of the Ondatechangedlistener listener is started<br>When the user changes the time and tick in the timepicker, the Ontimechangedlistener Listener's ontimechanged event will be started<br>When the user changes the year, month, and day in the datepickerdialog, the Ondateset () event of the Ondatesetlistener listener is started<br>When the user changes the time and tick in the timepickerdialog, the Ontimesetlistener listener's ontimeset () event will be started</p></p><p><p>Requirements function<br>The current date and event are displayed by default in the title bar, and the listener gets the result of the user's choice of date or event and displays the date or time of the change on the title bar<br>Set the caption by Settitle ().</p></p><p><p>DatePicker real-time change year, month, day:</p></p><pre><pre> DatePicker =<span style="color: #000000;"> (datePicker) findviewbyid (r.id.datepicker1); </span> <span style="color: #0000ff;">New</span> <span style="color: #000000;">ondatechangedlistener () { @Override </span>public<span style="color: #0000ff;"></span><span style="color: #0000ff;">void</span><span style="color: #0000ff;">int</span><span style="color: #0000ff;">int</span> <span style="color: #000000;">monthofyear, </span> <span style="color: #0000ff;">int</span> <span style="color: #000000;">Dayofmonth) { settitle (string.format (</span>"%d-%02d-%02d", year, monthofyear+1<span style="color: #000000;">, dayofmonth)); } );</span></pre></pre><p><p>Timepicker Real-time update time, minutes:</p></p><pre><pre> Timepicker =<span style="color: #000000;"> (timepicker) Findviewbyid (r.id.timepicker1); Timepicker.setontimechangedlistener (</span><span style="color: #0000ff;">new</span> <span style="color: #000000;"> ontimechangedlistener () { @Override </span> <span style="color: #0000ff;"> Public</span><span style="color: #0000ff;">void</span><span style="color: #0000ff;">int</span><span style="color: #0000ff;">int</span> <span style="color: #000000;"> Minute) { settitle (string.format (</span>"%02d:%02d" <span style="color: #000000;">, hourofday, minute)); } );</span></pre></pre><p><p>Create a new Datepickerdialog to Show:</p></p><pre><pre> <span style="color: #0000ff;">New</span> Datepickerdialog (<span style="color: #0000ff;"></span>This<span style="color: #0000ff;">new</span> <span style="color: #000000;"> ondatesetlistener () { @Override </span>public<span style="color: #0000ff;"></span> <span style="color: #0000ff;"> void</span><span style="color: #0000ff;">int</span><span style="color: #0000ff;"></span><span style="color: #0000ff;"></span> int int<span style="color: #000000;"> Dayofmonth) { settitle (string.format (</span>"%d-%02d- %02d ", year, monthofyear+1<span style="color: #000000;">, dayofmonth)) , {} , year, month, day). show ();</span></pre></pre><p><p>Create a new Timepickerdialog to Show:</p></p><pre><pre> <span style="color: #0000ff;">New</span> Timepickerdialog (<span style="color: #0000ff;"></span>This<span style="color: #0000ff;">new</span> <span style="color: #000000;"> ontimesetlistener () { @Override </span>public<span style="color: #0000ff;"></span> <span style="color: #0000ff;"> void</span><span style="color: #0000ff;">int</span><span style="color: #0000ff;">int</span> <span style="color: #000000;"> Minute) { settitle (string.format (</span>"%02d:%02d"<span style="color: #000000;">, hourofday, minute)); } </span> <span style="color: #0000ff;">true</span>). Show ();</pre></pre><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.example.datepickertimepicker;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.Calendar;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.DatePickerDialog;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.DatePickerDialog.OnDateSetListener;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.TimePickerDialog;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.TimePickerDialog.OnTimeSetListener;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.Bundle;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.support.v7.app.ActionBarActivity;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.DatePicker;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.DatePicker.OnDateChangedListener;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.TimePicker;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.TimePicker.OnTimeChangedListener;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">actionbaractivity {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">DatePicker DatePicker; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Timepicker timepicker; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Calendar calendar; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;">year <span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">month; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;">day <span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">hour; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">minute; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Calendar</span></span>=<span style="color: #000000;"><span style="color: #000000;">calendar.getinstance (); </span>year</span>=<span style="color: #000000;"><span style="color: #000000;">Calendar.get (calendar.year); Month</span></span>=<span style="color: #000000;"><span style="color: #000000;">Calendar.get (calendar.month); </span>day</span>=<span style="color: #000000;"><span style="color: #000000;">Calendar.get (calendar.day_of_month); Hour</span></span>=<span style="color: #000000;"><span style="color: #000000;">Calendar.get (calendar.hour_of_day); Minute</span></span>=<span style="color: #000000;"><span style="color: #000000;">Calendar.get (calendar.minute); Settitle (string.format (</span></span>"%d-%02d-%02d%02d:%02d", year, month+1<span style="color: #000000;"><span style="color: #000000;">, day, hour, minute)); DatePicker</span></span>=<span style="color: #000000;"><span style="color: #000000;">(DatePicker) Findviewbyid (r.id.datepicker1); Datepicker.init (year, month, day,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Ondatechangedlistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Ondatechanged (DatePicker view,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Year<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">monthofyear,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Dayofmonth) {settitle (string.format (</span></span>"%d-%02d-%02d", year, monthofyear+1<span style="color: #000000;"><span style="color: #000000;">, dayofmonth)); } }); Timepicker</span></span>=<span style="color: #000000;"><span style="color: #000000;">(timepicker) Findviewbyid (r.id.timepicker1); Timepicker.setontimechangedlistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Ontimechangedlistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Ontimechanged (timepicker view,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>hourofday,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Minute) {settitle (string.format (</span></span>"%02d:%02d"<span style="color: #000000;"><span style="color: #000000;">, hourofday, minute)); } }); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Datepickerdialog (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>,<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Ondatesetlistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Ondateset (DatePicker view,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Year<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>monthofyear,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Dayofmonth) {settitle (string.format (</span></span>"%d-%02d-%02d", year, monthofyear+1<span style="color: #000000;"><span style="color: #000000;">, dayofmonth)); }}, year, month, day). show (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Timepickerdialog (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>,<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Ontimesetlistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Ontimeset (timepicker view,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>hourofday,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Minute) {settitle (string.format (</span></span>"%02d:%02d"<span style="color: #000000;"><span style="color: #000000;">, hourofday, minute)); }}, hour, minute</span> ,</span><span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">). Show (); }}</span></span></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">Mainactivity.java</span></span><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">xmlns:android</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Xmlns:tools</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/tools"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:orientation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "vertical"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">DatePicker</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/datepicker1"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Timepicker</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/timepicker1"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">Activity_main.xml</span></span><p><p>Precautions:<br>The 1.DatePicker object takes the Init () method to specify the events for the DatePicker initial year, month, day, and Ondatechangedlistener () Whereas the Timepicker object is the action to be taken by the Event-altering program directly with the Setontimechangedlistener () Event.<br>The biggest difference between 2.DatePickerDialog and Timepickerdialog and the previous two types of objects is that DatePicker and Timepicker are displayed directly on the screen, The Datepickerdialog and Timepickerdialog objects are displayed in a popup dialog manner.</p></p><p><p>Effect:</p></p><p><p></p></p><p><p></p></p><p><p>Android uses DatePicker and timepicker to display the current date and time</p></p></span>

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.