DatePickerDialog.java revision 9f086d812ff04703de4b14e02a7702ba7c8acb33
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.Bundle; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.widget.DatePicker; 28import android.widget.DatePicker.OnDateChangedListener; 29 30/** 31 * A simple dialog containing an {@link android.widget.DatePicker}. 32 * 33 * <p>See the <a href="{@docRoot}resources/tutorials/views/hello-datepicker.html">Date Picker 34 * tutorial</a>.</p> 35 */ 36public class DatePickerDialog extends AlertDialog implements OnClickListener, 37 OnDateChangedListener { 38 39 private static final String YEAR = "year"; 40 private static final String MONTH = "month"; 41 private static final String DAY = "day"; 42 43 private final DatePicker mDatePicker; 44 private final OnDateSetListener mCallBack; 45 46 /** 47 * The callback used to indicate the user is done filling in the date. 48 */ 49 public interface OnDateSetListener { 50 51 /** 52 * @param view The view associated with this listener. 53 * @param year The year that was set. 54 * @param monthOfYear The month that was set (0-11) for compatibility 55 * with {@link java.util.Calendar}. 56 * @param dayOfMonth The day of the month that was set. 57 */ 58 void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth); 59 } 60 61 /** 62 * @param context The context the dialog is to run in. 63 * @param callBack How the parent is notified that the date is set. 64 * @param year The initial year of the dialog. 65 * @param monthOfYear The initial month of the dialog. 66 * @param dayOfMonth The initial day of the dialog. 67 */ 68 public DatePickerDialog(Context context, 69 OnDateSetListener callBack, 70 int year, 71 int monthOfYear, 72 int dayOfMonth) { 73 this(context, 0, callBack, year, monthOfYear, dayOfMonth); 74 } 75 76 /** 77 * @param context The context the dialog is to run in. 78 * @param theme the theme to apply to this dialog 79 * @param callBack How the parent is notified that the date is set. 80 * @param year The initial year of the dialog. 81 * @param monthOfYear The initial month of the dialog. 82 * @param dayOfMonth The initial day of the dialog. 83 */ 84 public DatePickerDialog(Context context, 85 int theme, 86 OnDateSetListener callBack, 87 int year, 88 int monthOfYear, 89 int dayOfMonth) { 90 super(context, theme); 91 92 mCallBack = callBack; 93 94 Context themeContext = getContext(); 95 setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_set), this); 96 setButton(BUTTON_NEGATIVE, themeContext.getText(R.string.cancel), (OnClickListener) null); 97 setIcon(0); 98 setTitle(R.string.date_picker_dialog_title); 99 100 LayoutInflater inflater = 101 (LayoutInflater) themeContext.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 /** 122 * Gets the {@link DatePicker} contained in this dialog. 123 * 124 * @return The calendar view. 125 */ 126 public DatePicker getDatePicker() { 127 return mDatePicker; 128 } 129 130 /** 131 * Sets the current date. 132 * 133 * @param year The date year. 134 * @param monthOfYear The date month. 135 * @param dayOfMonth The date day of month. 136 */ 137 public void updateDate(int year, int monthOfYear, int dayOfMonth) { 138 mDatePicker.updateDate(year, monthOfYear, dayOfMonth); 139 } 140 141 @Override 142 public Bundle onSaveInstanceState() { 143 Bundle state = super.onSaveInstanceState(); 144 state.putInt(YEAR, mDatePicker.getYear()); 145 state.putInt(MONTH, mDatePicker.getMonth()); 146 state.putInt(DAY, mDatePicker.getDayOfMonth()); 147 return state; 148 } 149 150 @Override 151 public void onRestoreInstanceState(Bundle savedInstanceState) { 152 super.onRestoreInstanceState(savedInstanceState); 153 int year = savedInstanceState.getInt(YEAR); 154 int month = savedInstanceState.getInt(MONTH); 155 int day = savedInstanceState.getInt(DAY); 156 mDatePicker.init(year, month, day, this); 157 } 158} 159