android中DatePicker&TimePicker的应用

布局:

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="设置日期"
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="设置时间"
28 />
29
30 </LinearLayout>

代码:

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 //声明一个独一无二的标识,来作为要显示DatePicker的Dialog的ID
44 final static int DATE_DIALOG_ID = 1;
45 //声明一个独一无二的标识,来作为要显示TimePicker的Dialog的ID
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 //调用Activity类的方法来显示Dialog:调用这个方法会允许Activity管理该Dialog的生命周期,
60 //并会调用 onCreateDialog(int)回调函数来请求一个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 //调用Activity类的方法来显示Dialog:调用这个方法会允许Activity管理该Dialog的生命周期,
70 //并会调用 onCreateDialog(int)回调函数来请求一个Dialog
71 showDialog(TIME_DIALOG_ID);
72 }
73 });
74 //得到当前日期和时间(作为初始值)
75 Calendar 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 //当Activity调用showDialog函数时会触发该函数的调用:
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 + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
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 + "时" + minute + "分");
106 }
107 }, hour, minute, true);
108 }
109 return null;
110 }
111
112
113
114 }




posted @ 2012-03-01 18:44  天天_byconan  阅读(1642)  评论(0编辑  收藏  举报