Android datepicker and TimePicker: time date selector

The Android SDK provides datepicker and TimePicker components to select date and time respectively to facilitate date and time setting. The following is a simple example to explain the use of datepicker and TimePicker components. Add a button named "date / time" in the layout file main.xml of the project widgetdemo to start timeactivity. Add the following code in main.xml:

Click button and start timeactivity as follows:

At the same time, declare the activity in the androidmanifest.xml file:

<activity android:name=" .TimeActivity"></activity>

The running effect of timeactivity is shown in Figure 1.

The layout file used by timeactivity is time.xml, which is as follows:

The code of timeactivity.java is as follows:

Use the java.util.calendar object in timeactivity to get the current system time. When changing the date in the datepicker component, the ondatechange() event of datepicker will be triggered; When the time of timepacker is modified, the ondatechange() event of timepacker will be triggered. It can be seen from this example that the method of datepicker implementing ondatechangedlistener listener is similar to that of TimePicker implementing setontimechangedlistener listener. Datepicker uses the init () method to set the year, month, and day, while TimePicker uses setontimechangedlistener () to set the listener directly.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>