Android enables time selection via Dialogfragment

Source: Internet
Author: User

Time controls are an integral part of Android development, especially when setting up a personal birthday or searching by time. Android has built-in DatePicker and Timepicker, which is also quite handy, either after adding a Findviewbyid call to the layout or overriding oncreatedialog directly in the activity (int ID) method called ShowDialog (int id) pop-up, and now the online demo of the Android Time control is mostly based on the instructions for using the two controls. But people who have used both controls know that these two time boxes have two less-than-good places: 1, not particularly beautiful 2, the time control life cycle is not controllable. If we want to solve the problem above, we will generally write a beautiful and satisfying time control by inheriting dialog. But it certainly takes much more time than using DatePicker and Timepicker.

Of course, there are still a lot of good open source time controls available on the web, such as Android-wheel, which is a time selection control (address: Https://github.com/maarek/android-wheel) that imitates the iOS wheel style, as follows:

Android-spinnerwheel is also a more than the wrong time to select the control (address: Https://github.com/ai212983/android-spinnerwheel), as follows:

 

People who have used DatePicker and timepicker should know that ShowDialog (int id) is actually an outdated method, and in the description of the method we can find such a phrase: This method is deprecated in API Level 13.Use, the new Dialogfragment class with Fragmentmanager instead; This was also available on older platforms through the Android compatibility package.

It probably means: showDialog (int id) This method is obsolete in API 13, the new API can be replaced by using the Dialogfragment class and Fragmentmanager. Dailogfragment and Fragmentmanager are also available in the version API (less than 13), but the compatibility pack needs to be introduced.

Let's take a look at how the selection time is achieved through dialogfragment and Fragmentmanager, and what are the advantages of it relative to the ShowDialog (int id)?

We first go to the Android developer official website, where the official website has such a sentence: The dialogfragment manages the dialog lifecycle for you and allows your to display the Picke RS in different layout configurations, such as in a basic dialog on handsets or as an embedded part of the layout on large Screens.

The idea is that dialogfragment can let us manage the life cycle of the time selection control and let us set different configuration parameters for the control, that is, the control can be displayed as a basic shape, or it can be placed in a layout that is displayed on a large screen phone.

Similarly, the use of official Internet dialogfragment is also very convenient. Let's take a look at the following:

First of all, we need to customize a fragement and let him inherit dialogfragment, and it needs to be implemented Timepickerdialog.ontimesetlistener

interface, the code is as follows:

  

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{    int _year=1970;     int _month=0;     int _day=0;         @Override     public Dialog onCreateDialog(Bundle savedInstanceState) {         final Calendar c=Calendar.getInstance();         int year=c.get(Calendar.YEAR);         int month=c.get(Calendar.MONTH);         int day=c.get(Calendar.DAY_OF_MONTH);         return new DatePickerDialog(getActivity(), this, year, month, day);     }         @Override     public void onDateSet(DatePicker view, int year, int monthOfYear,             int dayOfMonth) {         // TODO 日期选择完成事件,取消时不会触发         _year=year;         _month=monthOfYear+1;         _day=dayOfMonth;         Log.i(Constant.LOG_TAG, "year="+year+",monthOfYear="+monthOfYear+",dayOfMonth="+dayOfMonth);     }    private String getValue(){         return ""+_year+_month+_day;     }     }

When we click on a control to pop up the time selector, just call the following method directly.

?
1 2 3 4 private void showDatePickerFragemnt(){     DialogFragment fragment=new DatePickerFragment();     fragment.show(getSupportFragmentManager(), "datePicker");}

It is important to note that your activity needs to inherit fragmentactivity or actionbaractivity, otherwise it will not get fragmentmanager.

More Dialogfragment Use the description address, we can go to http://developer.android.com/guide/topics/ui/controls/pickers.html.

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.