DatePickerDialog.java revision bf80562d22b2bbe7944d80d0524c69d0238010cb
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.app; 18 19import com.android.internal.R; 20 21import android.content.Context; 22import android.content.DialogInterface; 23import android.content.DialogInterface.OnClickListener; 24import android.os.Build; 25import android.os.Bundle; 26import android.view.LayoutInflater; 27import android.view.View; 28import android.widget.DatePicker; 29import android.widget.DatePicker.OnDateChangedListener; 30 31/** 32 * A simple dialog containing an {@link android.widget.DatePicker}. 33 * 34 * <p>See the <a href="{@docRoot}resources/tutorials/views/hello-datepicker.html">Date Picker 35 * tutorial</a>.</p> 36 */ 37public class DatePickerDialog extends AlertDialog implements OnClickListener, 38 OnDateChangedListener { 39 40 private static final String YEAR = "year"; 41 private static final String MONTH = "month"; 42 private static final String DAY = "day"; 43 44 private final DatePicker mDatePicker; 45 private final OnDateSetListener mCallBack; 46 47 /** 48 * The callback used to indicate the user is done filling in the date. 49 */ 50 public interface OnDateSetListener { 51 52 /** 53 * @param view The view associated with this listener. 54 * @param year The year that was set. 55 * @param monthOfYear The month that was set (0-11) for compatibility 56 * with {@link java.util.Calendar}. 57 * @param dayOfMonth The day of the month that was set. 58 */ 59 void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth); 60 } 61 62 /** 63 * @param context The context the dialog is to run in. 64 * @param callBack How the parent is notified that the date is set. 65 * @param year The initial year of the dialog. 66 * @param monthOfYear The initial month of the dialog. 67 * @param dayOfMonth The initial day of the dialog. 68 */ 69 public DatePickerDialog(Context context, 70 OnDateSetListener callBack, 71 int year, 72 int monthOfYear, 73 int dayOfMonth) { 74 this(context, 0, callBack, year, monthOfYear, dayOfMonth); 75 } 76 77 /** 78 * @param context The context the dialog is to run in. 79 * @param theme the theme to apply to this dialog 80 * @param callBack How the parent is notified that the date is set. 81 * @param year The initial year of the dialog. 82 * @param monthOfYear The initial month of the dialog. 83 * @param dayOfMonth The initial day of the dialog. 84 */ 85 public DatePickerDialog(Context context, 86 int theme, 87 OnDateSetListener callBack, 88 int year, 89 int monthOfYear, 90 int dayOfMonth) { 91 super(context, theme); 92 93 mCallBack = callBack; 94 95 setButton(BUTTON_POSITIVE, context.getText(R.string.date_time_set), this); 96 setButton(BUTTON_NEGATIVE, context.getText(R.string.cancel), (OnClickListener) null); 97 setIcon(0); 98 setTitle(R.string.date_picker_dialog_title); 99 100 LayoutInflater inflater = 101 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 102 View view = inflater.inflate(R.layout.date_picker_dialog, null); 103 setView(view); 104 mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); 105 mDatePicker.init(year, monthOfYear, dayOfMonth, this); 106 } 107 108 public void onClick(DialogInterface dialog, int which) { 109 if (mCallBack != null) { 110 mDatePicker.clearFocus(); 111 mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(), 112 mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); 113 } 114 } 115 116 public void onDateChanged(DatePicker view, int year, 117 int month, int day) { 118 mDatePicker.init(year, month, day, null); 119 } 120 121 public void updateDate(int year, int monthOfYear, int dayOfMonth) { 122 mDatePicker.updateDate(year, monthOfYear, dayOfMonth); 123 } 124 125 @Override 126 public Bundle onSaveInstanceState() { 127 Bundle state = super.onSaveInstanceState(); 128 state.putInt(YEAR, mDatePicker.getYear()); 129 state.putInt(MONTH, mDatePicker.getMonth()); 130 state.putInt(DAY, mDatePicker.getDayOfMonth()); 131 return state; 132 } 133 134 @Override 135 public void onRestoreInstanceState(Bundle savedInstanceState) { 136 super.onRestoreInstanceState(savedInstanceState); 137 int year = savedInstanceState.getInt(YEAR); 138 int month = savedInstanceState.getInt(MONTH); 139 int day = savedInstanceState.getInt(DAY); 140 mDatePicker.init(year, month, day, this); 141 } 142} 143