samedi 18 avril 2015

I use a customized DialogFragment which contains <DatePicker> and <TimePicker> elements. I retrieve the instances of this UI widgets via findViewById. The problem is, I don't know how to set the listeners for DatePicker detecting date change.It goes the same with TimePicker.


Here is my code:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/dialog_fragment_layout_datetimepicker"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:orientation="vertical" >

<DatePicker
android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TimePicker
android:id="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/button_datepickersave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dialog_button_save"/>

</LinearLayout>


I retrieve the DatePicker and TimePicker elements via:



mDateTimePickerDialog = new Dialog(getActivity());
mDateTimePickerDialog.setContentView(R.layout.dialog_fragment_datetimepicker);
mDateTimePickerDialog.setTitle(R.string.title_choose_datetime);

mDatePicker = (DatePicker) mDateTimePickerDialog.findViewById(R.id.datepicker);
mTimePicker = (TimePicker) mDateTimePickerDialog.findViewById(R.id.timepicker);


But upon looking on the documentation, I only see a way if DatePicker and TimePicker is all dialog by themselves and set the listeners on their constructors.


How can I add listener on DatePicker and TimePicker created on this way? Is this possible? Are there alternate way to do this? Is this best done using third-party source?


Thank you.


Aucun commentaire:

Enregistrer un commentaire