Android date and time selection control datepicker and TimePicker instances

This month, I made a time selector in the project according to the needs. Although I did not use the Android native time selection control, I was ashamed to find that I had never used this control! While you have time now, check and fill in the gaps and write a blog.

(Note: for the convenience of distinction, the control for selecting month, year and day is called date selection control, and the control for selecting time division is called time selection control.)

1. Create project

Create a new project. The layout of mainactivity is as follows:

The interface effect is as follows:

After clicking the entry, the date or time selection control will pop up. After clicking the OK button, the selection result will be displayed in the light gray box.

2. Initializing controls and creating related variables

2.1 initializing controls

Initialize the control and set listening events for the two linearlayouts:

2.2 creating related variables

Use the calendar class to get the current date and time.

Note that the month subscript obtained by calendar.get (calendar. Month) starts from 0. When the value is 0, it means January, when 1, it means February, and so on, so 1 must be added.

Create two StringBuffer variables to splice the acquired time data.

3. Date selection control datepicker

Here, we choose the form of custom alertdialog to display the selection control.

Date selection control custom layout dialog_ Date.xml is as follows:

Just place a datepicker control. The appearance of datepicker control in different versions of Android may be different. For example, on Android 4.4, it is in the form of scroll wheel, and on 7.0, it is a calendar view. In order to avoid taking up too much space to display the calendar view in earlier versions such as 4.4, you can set the Android: calendarviewshow property to fasle.

Inherit the interface datepicker.ondatechangedlistener to implement the date change listening method:

In this way, you can get the year, month and day value selected by the user. Here is how to create the alertdialog:

Datepicker needs to call the init method for initialization, and pass in the year, month and day value and ondatechangedlistener object. Remember that when we assigned a value to month, we already had the corresponding month value, and what we need here is the subscript value, so we need to subtract 1.

After running, the effect is as shown in the figure:

4. Time selection control TimePicker

Here we also need a custom alertdialog layout:

Similarly, we need to monitor time and minute changes, so we inherit the timepicker.ontimechangedlistener interface and implement the following methods:

The initialization of the time selection control is different from that of the date selection control. Datepicker uses an init method, but TimePicker has a slightly larger workload. Look at the following code:

In addition to setting the hours and minutes currently to be displayed, you should also set whether it is a 24-hour system or a 12 hour system.

The renderings are as follows:

5. Complete mainactivity code

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>