Android implements the time selection function

preface

Since most of the initial Android tutorials don't teach you how to choose a time, beginners will inevitably be at a loss when they encounter this. Do you want users to enter the date and time themselves? Not to mention the poor user experience, it takes a lot of effort to deal with various date formats entered by users. Let's see how to realize the function of selecting time on Android.

Running effect in Android 6.0

Introduction to timepickerdialog and DatePickerDialog

The system encapsulates two classes for us to call directly. Timepickerdialog is used to select the time and DatePickerDialog is used to select the date.

Construction method of timepikckerdialog

1. The first parameter accepts a context message

2. The second parameter is the callback interface to be executed after the selected time is completed

3. The third and fourth parameters are the initialization time

4. For the fourth parameter, select true for 24-hour system and false for 12 hour system

DatePickerDialog constructor

1. The first parameter accepts context information

2. The second parameter is the drop back interface after date selection

3. The last three parameters are the year, month and day of initialization

It can be seen that the construction methods of the two are basically the same. Since both are inherited from alertdialog, after obtaining their objects, you can call their show () method to pop up the selection box.

Concrete implementation

There are two implementation methods: one is to use it directly in the activity, and the other is to use it through the fragmentdialog.

It's easy to use directly in activity, but the code will be messy. The use method managed through fragmentdialog will be elegant and easy to manage.

Use directly in activity

In the layout file, a textview is used to display the selected time

Activity file:

Here, click Run to see the effect:)

Using the fragmentdialog

Why use dialogfragment

1. The use of dialogfragment to manage dialog boxes is officially recommended.

2. Using dialog fragment to manage dialog boxes also facilitates code reuse.

The steps are implemented through the fragmentdialog

Datepickerfragment class:

Timepickerfragment class:

The layout file of activity has only one textview to display the time

Activity file:

Since the timepickerfragment dialog box is started in the datepickerfragment class, you can only select both date and time in this way. If you want to select a date or time separately, you only need to override ontimeset() or ondateset() methods

Compatibility issues

Different Android versions display different effects, in Android 6 0 works well, but in some low versions of Android (such as 4.0, which the author did not test every version), the function will be called back twice, resulting in the selection of the time twice. There are many solutions, as long as you ensure that the logic in the callback function is executed only once. A more general method is provided here.

Override the onstop() method of timepickerdialog and DatePickerDialog

Override method used directly in activity

The above writing method looks messy. You can also create another class to inherit timepickerdialog or DatePickerDialog, and then override the onstop () method

Override method used by fragmentdialog

Just rewrite it in the oncreatedialog () method, and the following code will be clearer

summary

The author's level is limited, but ensure that the above code is implemented by hand. If there are any shortcomings, you are welcome to point out. The above is the whole content of this article. I hope it can be helpful for you to develop Android.

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