DatePickerDialog.java revision fe41ce4ef97e7da585fdbf06988da8047e72b9f6
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_done), this); 96 setIcon(0); 97 setTitle(R.string.date_picker_dialog_title); 98 99 LayoutInflater inflater = 100 (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 101 View view = inflater.inflate(R.layout.date_picker_dialog, null); 102 setView(view); 103 mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); 104 mDatePicker.init(year, monthOfYear, dayOfMonth, this); 105 } 106 107 public void onClick(DialogInterface dialog, int which) { 108 tryNotifyDateSet(); 109 } 110 111 public void onDateChanged(DatePicker view, int year, 112 int month, int day) { 113 mDatePicker.init(year, month, day, null); 114 } 115 116 /** 117 * Gets the {@link DatePicker} contained in this dialog. 118 * 119 * @return The calendar view. 120 */ 121 public DatePicker getDatePicker() { 122 return mDatePicker; 123 } 124 125 /** 126 * Sets the current date. 127 * 128 * @param year The date year. 129 * @param monthOfYear The date month. 130 * @param dayOfMonth The date day of month. 131 */ 132 public void updateDate(int year, int monthOfYear, int dayOfMonth) { 133 mDatePicker.updateDate(year, monthOfYear, dayOfMonth); 134 } 135 136 private void tryNotifyDateSet() { 137 if (mCallBack != null) { 138 mDatePicker.clearFocus(); 139 mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(), 140 mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); 141 } 142 } 143 144 @Override 145 protected void onStop() { 146 tryNotifyDateSet(); 147 super.onStop(); 148 } 149 150 @Override 151 public Bundle onSaveInstanceState() { 152 Bundle state = super.onSaveInstanceState(); 153 state.putInt(YEAR, mDatePicker.getYear()); 154 state.putInt(MONTH, mDatePicker.getMonth()); 155 state.putInt(DAY, mDatePicker.getDayOfMonth()); 156 return state; 157 } 158 159 @Override 160 public void onRestoreInstanceState(Bundle savedInstanceState) { 161 super.onRestoreInstanceState(savedInstanceState); 162 int year = savedInstanceState.getInt(YEAR); 163 int month = savedInstanceState.getInt(MONTH); 164 int day = savedInstanceState.getInt(DAY); 165 mDatePicker.init(year, month, day, this); 166 } 167} 168