本文主要讲述Android中的日期控件和时间控件的使用,以一个Demo的例子来展示日期和时间控件的使用,先看下如下效果图:
从效果图中可以看到该Demo是通过单击【选择日期】按钮和【选择时间】按钮弹出日期或者时间的对话框,然后设置日期或者时间,设置完成后会在文本框中显示设置的日期或时间值。
【1】Demo程序框架图:
【2】布局文件 res/layout/main.xml 源码:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" > |
07 | android:layout_width = "fill_parent" android:layout_height = "wrap_content" |
08 | android:gravity = "center" android:text = "欢迎关注Andy.Chen Blog" /> |
10 | android:layout_width = "fill_parent" android:layout_height = "wrap_content" |
11 | android:gravity = "center" android:text = "日期和时间控件的使用DEMO" /> |
13 | < LinearLayout android:orientation = "horizontal" |
14 | android:layout_width = "fill_parent" android:layout_height = "wrap_content" > |
16 | < EditText android:id = "@+id/showdate" android:layout_width = "fill_parent" |
17 | android:layout_height = "wrap_content" android:layout_weight = "1" /> |
18 | < Button android:id = "@+id/pickdate" android:layout_width = "wrap_content" |
19 | android:layout_height = "wrap_content" android:text = "选择日期" /> |
23 | < LinearLayout android:orientation = "horizontal" |
24 | android:layout_width = "fill_parent" android:layout_height = "wrap_content" > |
26 | < EditText android:id = "@+id/showtime" android:layout_width = "fill_parent" |
27 | android:layout_height = "wrap_content" android:layout_weight = "1" /> |
28 | < Button android:id = "@+id/picktime" android:layout_width = "wrap_content" |
29 | android:layout_height = "wrap_content" android:text = "选择时间" /> |
【3】包com.andyidea.calenderdemo下MainActivity.java源码:
001 | package com.andyidea.calenderdemo; |
003 | import java.util.Calendar; |
005 | import android.app.Activity; |
006 | import android.app.DatePickerDialog; |
007 | import android.app.Dialog; |
008 | import android.app.TimePickerDialog; |
009 | import android.os.Bundle; |
010 | import android.os.Handler; |
011 | import android.os.Message; |
012 | import android.view.View; |
013 | import android.widget.Button; |
014 | import android.widget.DatePicker; |
015 | import android.widget.EditText; |
016 | import android.widget.TimePicker; |
018 | public class MainActivity extends Activity { |
020 | private EditText showDate = null ; |
021 | private Button pickDate = null ; |
022 | private EditText showTime = null ; |
023 | private Button pickTime = null ; |
025 | private static final int SHOW_DATAPICK = 0 ; |
026 | private static final int DATE_DIALOG_ID = 1 ; |
027 | private static final int SHOW_TIMEPICK = 2 ; |
028 | private static final int TIME_DIALOG_ID = 3 ; |
036 | /** Called when the activity is first created. */ |
038 | public void onCreate(Bundle savedInstanceState) { |
039 | super .onCreate(savedInstanceState); |
040 | setContentView(R.layout.main); |
044 | final Calendar c = Calendar.getInstance(); |
045 | mYear = c.get(Calendar.YEAR); |
046 | mMonth = c.get(Calendar.MONTH); |
047 | mDay = c.get(Calendar.DAY_OF_MONTH); |
049 | mHour = c.get(Calendar.HOUR_OF_DAY); |
050 | mMinute = c.get(Calendar.MINUTE); |
059 | private void initializeViews(){ |
060 | showDate = (EditText) findViewById(R.id.showdate); |
061 | pickDate = (Button) findViewById(R.id.pickdate); |
062 | showTime = (EditText)findViewById(R.id.showtime); |
063 | pickTime = (Button)findViewById(R.id.picktime); |
065 | pickDate.setOnClickListener( new View.OnClickListener() { |
068 | public void onClick(View v) { |
069 | Message msg = new Message(); |
070 | if (pickDate.equals((Button) v)) { |
071 | msg.what = MainActivity.SHOW_DATAPICK; |
073 | MainActivity. this .dateandtimeHandler.sendMessage(msg); |
077 | pickTime.setOnClickListener( new View.OnClickListener() { |
080 | public void onClick(View v) { |
081 | Message msg = new Message(); |
082 | if (pickTime.equals((Button) v)) { |
083 | msg.what = MainActivity.SHOW_TIMEPICK; |
085 | MainActivity. this .dateandtimeHandler.sendMessage(msg); |
093 | private void setDateTime(){ |
094 | final Calendar c = Calendar.getInstance(); |
096 | mYear = c.get(Calendar.YEAR); |
097 | mMonth = c.get(Calendar.MONTH); |
098 | mDay = c.get(Calendar.DAY_OF_MONTH); |
106 | private void updateDateDisplay(){ |
107 | showDate.setText( new StringBuilder().append(mYear).append( "-" ) |
108 | .append((mMonth + 1 ) < 10 ? "0" + (mMonth + 1 ) : (mMonth + 1 )).append( "-" ) |
109 | .append((mDay < 10 ) ? "0" + mDay : mDay)); |
115 | private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { |
117 | public void onDateSet(DatePicker view, int year, int monthOfYear, |
120 | mMonth = monthOfYear; |
130 | private void setTimeOfDay(){ |
131 | final Calendar c = Calendar.getInstance(); |
132 | mHour = c.get(Calendar.HOUR_OF_DAY); |
133 | mMinute = c.get(Calendar.MINUTE); |
140 | private void updateTimeDisplay(){ |
141 | showTime.setText( new StringBuilder().append(mHour).append( ":" ) |
142 | .append((mMinute < 10 ) ? "0" + mMinute : mMinute)); |
148 | private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { |
151 | public void onTimeSet(TimePicker view, int hourOfDay, int minute) { |
160 | protected Dialog onCreateDialog( int id) { |
163 | return new DatePickerDialog( this , mDateSetListener, mYear, mMonth, |
166 | return new TimePickerDialog( this , mTimeSetListener, mHour, mMinute, true ); |
173 | protected void onPrepareDialog( int id, Dialog dialog) { |
176 | ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); |
179 | ((TimePickerDialog) dialog).updateTime(mHour, mMinute); |
187 | Handler dateandtimeHandler = new Handler() { |
190 | public void handleMessage(Message msg) { |
192 | case MainActivity.SHOW_DATAPICK: |
193 | showDialog(DATE_DIALOG_ID); |
195 | case MainActivity.SHOW_TIMEPICK: |
196 | showDialog(TIME_DIALOG_ID); |
【4】程序运行效果图:
转自:http://blog.csdn.net/cjjky/article/details/7292445