DatePickerDialog.java revision 50f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5c
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, context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB 75 ? com.android.internal.R.style.Theme_Holo_Dialog_Alert 76 : com.android.internal.R.style.Theme_Dialog_Alert, 77 callBack, year, monthOfYear, dayOfMonth); 78 } 79 80 /** 81 * @param context The context the dialog is to run in. 82 * @param theme the theme to apply to this dialog 83 * @param callBack How the parent is notified that the date is set. 84 * @param year The initial year of the dialog. 85 * @param monthOfYear The initial month of the dialog. 86 * @param dayOfMonth The initial day of the dialog. 87 */ 88 public DatePickerDialog(Context context, 89 int theme, 90 OnDateSetListener callBack, 91 int year, 92 int monthOfYear, 93 int dayOfMonth) { 94 super(context, theme); 95 96 mCallBack = callBack; 97 98 setButton(BUTTON_POSITIVE, context.getText(R.string.date_time_set), this); 99 setButton(BUTTON_NEGATIVE, context.getText(R.string.cancel), (OnClickListener) null); 100 setIcon(0); 101 setTitle(R.string.date_picker_dialog_title); 102 103 LayoutInflater inflater = 104 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 105 View view = inflater.inflate(R.layout.date_picker_dialog, null); 106 setView(view); 107 mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); 108 mDatePicker.init(year, monthOfYear, dayOfMonth, this); 109 } 110 111 public void onClick(DialogInterface dialog, int which) { 112 if (mCallBack != null) { 113 mDatePicker.clearFocus(); 114 mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(), 115 mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); 116 } 117 } 118 119 public void onDateChanged(DatePicker view, int year, 120 int month, int day) { 121 mDatePicker.init(year, month, day, null); 122 } 123 124 public void updateDate(int year, int monthOfYear, int dayOfMonth) { 125 mDatePicker.updateDate(year, monthOfYear, dayOfMonth); 126 } 127 128 @Override 129 public Bundle onSaveInstanceState() { 130 Bundle state = super.onSaveInstanceState(); 131 state.putInt(YEAR, mDatePicker.getYear()); 132 state.putInt(MONTH, mDatePicker.getMonth()); 133 state.putInt(DAY, mDatePicker.getDayOfMonth()); 134 return state; 135 } 136 137 @Override 138 public void onRestoreInstanceState(Bundle savedInstanceState) { 139 super.onRestoreInstanceState(savedInstanceState); 140 int year = savedInstanceState.getInt(YEAR); 141 int month = savedInstanceState.getInt(MONTH); 142 int day = savedInstanceState.getInt(DAY); 143 mDatePicker.init(year, month, day, this); 144 } 145} 146