Layout:
View code
1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
3 Android: layout_width = "fill_parent"
4 Android: layout_height = "fill_parent"
5 Android: Orientation = "vertical">
6
7 <textview
8 Android: Id = "@ + ID/datetext"
9 Android: layout_width = "wrap_content"
10 Android: layout_height = "wrap_content"
11/>
12 <button
13 Android: Id = "@ + ID/datebutton"
14 Android: layout_width = "fill_parent"
15 Android: layout_height = "wrap_content"
16 Android: text = "set date"
17/>
18 <textview
19 Android: Id = "@ + ID/timetext"
20 Android: layout_width = "wrap_content"
21 Android: layout_height = "wrap_content"
22/>
23 <button
24 Android: Id = "@ + ID/timebutton"
25 Android: layout_width = "fill_parent"
26 Android: layout_height = "wrap_content"
27 Android: text = "set time"
28/>
29
30 </linearlayout>
Code:
View code
1 package com. Tiantian. test;
2
3 Import java. util. calendar;
4
5 import Android. App. activity;
6 Import Android. App. datepickerdialog;
7 Import Android. App. datepickerdialog. ondatesetlistener;
8 Import Android. App. Dialog;
9 Import Android. App. timepickerdialog;
10 Import Android. App. timepickerdialog. ontimesetlistener;
11 import Android. content. intent;
12 Import Android. Graphics. Bitmap;
13 Import Android. Graphics. bitmapfactory;
14 Import Android. Graphics. matrix;
15 Import android.net. Uri;
16 Import Android. OS. Bundle;
17 import Android. OS. systemclock;
18 Import Android. util. displaymetrics;
19 Import Android. View. view;
20 Import Android. View. View. onclicklistener;
21 import Android. widget. Button;
22 Import Android. widget. chronometer;
23 Import Android. widget. datepicker;
24 import Android. widget. datepicker. ondatechangedlistener;
25 import Android. widget. imageview;
26 Import Android. widget. textview;
27 import Android. widget. timepicker;
28 Import Android. widget. timepicker. ontimechangedlistener;
29 Import Android. widget. zoomcontrols;
30
31 public class textandridactivity extends activity {
32/** called when the activity is first created .*/
33 private textview datetext;
34 Private textview timetext;
35 private button datebutton;
36 private button timebutton;
37
38 private int year;
39 private int monthofyear;
40 private int dayofmonth;
41 private int hour;
42 private int minute;
43 // declare a unique identifier as the ID of the datepicker Dialog
44 final static int date_dialog_id = 1;
45 // declare a unique identifier as the ID of the dalog to display timepicker
46 final static int time_dialog_id = 2;
47 @ override
48 Public void oncreate (bundle savedinstancestate ){
49 super. oncreate (savedinstancestate );
50 setcontentview (R. layout. Main );
51
52 datetext = (textview) findviewbyid (R. Id. datetext );
53 timetext = (textview) findviewbyid (R. Id. timetext );
54 datebutton = (button) findviewbyid (R. Id. datebutton );
55 datebutton. setonclicklistener (New onclicklistener (){
56
57 @ override
58 public void onclick (view v ){
59 // call the method of the activity class to display the dialog: Calling This method allows the activity to manage the lifecycle of the dialog,
60 // and calls the oncreatedialog (INT) callback function to request a dialog
61 showdialog (date_dialog_id );
62}
63 });
64 timebutton = (button) findviewbyid (R. Id. timebutton );
65 timebutton. setonclicklistener (New onclicklistener (){
66
67 @ override
68 public void onclick (view v ){
69 // call the method of the activity class to display the dialog: Calling This method allows the activity to manage the lifecycle of the dialog,
70 // and calls the oncreatedialog (INT) callback function to request a dialog
71 showdialog (time_dialog_id );
72}
73 });
74 // obtain the current date and time (as the initial value)
75 calendar = calendar. getinstance ();
76 year = calendar. Get (calendar. year );
77 monthofyear = calendar. Get (calendar. month );
78 dayofmonth = calendar. Get (calendar. day_of_month );
79 hour = calendar. Get (calendar. hour_of_day );
80 minute = calendar. Get (calendar. Minute );
81
82}
83
84 // when the activity calls the showdialog function, the function is called:
85 @ override
86 protected dialog oncreatedialog (int id ){
87 // todo auto-generated method stub
88 switch (ID ){
89 case date_dialog_id:
90 return New datepickerdialog (this, new ondatesetlistener (){
91
92 @ override
93 public void ondateset (datepicker view, int year, int monthofyear,
94 int dayofmonth ){
95 // todo auto-generated method stub
96 datetext. settext (Year + "year" + (monthofyear + 1) + "month" + dayofmonth + "day ");
97}
98}, year, monthofyear, dayofmonth );
99 case time_dialog_id:
100 return New timepickerdialog (this, new ontimesetlistener (){
101
102 @ override
103 Public void ontimeset (timepicker view, int hourofday, int minute ){
104 // todo auto-generated method stub
105 timetext. settext (hourofday + "Hour" + minute + "Minute ");
106}
107}, hour, minute, true );
108}
109 return NULL;
110}
111
112
113
114}