1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.view.View;
import java.util.Calendar;
/**
* Basic example of using date and time widgets, including
* {@link android.app.TimePickerDialog} and {@link android.widget.DatePicker}.
*
* Also provides a good example of using {@link Activity#onCreateDialog},
* {@link Activity#onPrepareDialog} and {@link Activity#showDialog} to have the
* activity automatically save and restore the state of the dialogs.
*/
public class DateWidgets1 extends Activity {
// where we display the selected date and time
private TextView mDateDisplay;
// date and time
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
static final int TIME_12_DIALOG_ID = 0;
static final int TIME_24_DIALOG_ID = 1;
static final int DATE_DIALOG_ID = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_widgets_example_1);
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
setDialogOnClickListener(R.id.pickDate, DATE_DIALOG_ID);
setDialogOnClickListener(R.id.pickTime12, TIME_12_DIALOG_ID);
setDialogOnClickListener(R.id.pickTime24, TIME_24_DIALOG_ID);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
updateDisplay();
}
private void setDialogOnClickListener(int buttonId, final int dialogId) {
Button b = (Button) findViewById(buttonId);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(dialogId);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_12_DIALOG_ID:
case TIME_24_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, id == TIME_24_DIALOG_ID);
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case TIME_12_DIALOG_ID:
case TIME_24_DIALOG_ID:
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
break;
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" ")
.append(pad(mHour)).append(":")
.append(pad(mMinute)));
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
|