Najprawdopodobniej najlepiej przeczytać najpierw o numerach Dialogs
i Pickers
.
Jeśli chodzi o implementację, możesz mieć dwa przyciski: Jeden do wyboru wyboru daty dla daty początkowej i drugi do daty zakończenia.
Edycja: Jeśli naprawdę chcesz pokazać 2 selektory daty w jednym oknie dialogowym, oto przykład jak to zrobić. Najpierw utwórz niestandardowy układ XML.
/res/layout/custom_date_picker.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/dpStartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:calendarViewShown="false" />
<DatePicker
android:id="@+id/dpEndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:calendarViewShown="false" />
</LinearLayout>
Następny jest użycie powyższy układ w oknie dialogowym:
// These variables will hold the date values later
private int startYear, startMonth, startDay, endYear, endMonth, endDay;
/**
* Displays the start and end date picker dialog
*/
public void showDatePicker() {
// Inflate your custom layout containing 2 DatePickers
LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
View customView = inflater.inflate(R.layout.custom_date_picker, null);
// Define your date pickers
final DatePicker dpStartDate = (DatePicker) customView.findViewById(R.id.dpStartDate);
final DatePicker dpEndDate = (DatePicker) customView.findViewById(R.id.dpEndDate);
// Build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView); // Set the view of the dialog to your custom layout
builder.setTitle("Select start and end date");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
startYear = dpStartDate.getYear();
startMonth = dpStartDate.getMonth();
startDay = dpStartDate.getDayOfMonth();
endYear = dpEndDate.getYear();
endMonth = dpEndDate.getMonth();
endDay = dpEndDate.getDayOfMonth();
dialog.dismiss();
}});
// Create and show the dialog
builder.create().show();
}
Wreszcie, można wyświetlić to okno, po prostu dzwoniąc showDatePicker()
.
dodać dwa wyboru daty w xml dialogowego. Użyj OnDateChangedaListener do słuchania zmian. Użyj setNegativeButton dla "Anuluj" i setPositiveButton dla "Ok" – Milan