Android中日期和时间控件的使用

 本文主要讲述Android中的日期控件和时间控件的使用,以一个Demo的例子来展示日期和时间控件的使用,先看下如下效果图:

Android中日期和时间控件的使用

从效果图中可以看到该Demo是通过单击【选择日期】按钮和【选择时间】按钮弹出日期或者时间的对话框,然后设置日期或者时间,设置完成后会在文本框中显示设置的日期或时间值。

【1】Demo程序框架图:

Android中日期和时间控件的使用

【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">
06     <TextView
07        android:layout_width="fill_parent" android:layout_height="wrap_content"
08        android:gravity="center" android:text="欢迎关注Andy.Chen Blog" />
09     <TextView
10        android:layout_width="fill_parent" android:layout_height="wrap_content"
11        android:gravity="center" android:text="日期和时间控件的使用DEMO" /> 
12    
13     <LinearLayout android:orientation="horizontal" 
14        android:layout_width="fill_parent" android:layout_height="wrap_content"
15  
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="选择日期"/> 
20    
21     </LinearLayout>
22      
23     <LinearLayout android:orientation="horizontal" 
24        android:layout_width="fill_parent" android:layout_height="wrap_content"
25  
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="选择时间"/> 
30    
31     </LinearLayout>
32 </LinearLayout>
【3】包com.andyidea.calenderdemo下MainActivity.java源码:
001 package com.andyidea.calenderdemo;
002  
003 import java.util.Calendar;
004  
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;
017  
018 public class MainActivity extends Activity {
019      
020     private EditText showDate = null;
021     private Button pickDate = null;
022     private EditText showTime = null;
023     private Button pickTime = null;
024      
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;
029      
030     private int mYear; 
031     private int mMonth;
032     private int mDay;
033     private int mHour;
034     private int mMinute;
035      
036     /** Called when the activity is first created. */
037     @Override
038     public void onCreate(Bundle savedInstanceState) {
039         super.onCreate(savedInstanceState);
040         setContentView(R.layout.main);
041          
042         initializeViews();
043          
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);
048          
049         mHour = c.get(Calendar.HOUR_OF_DAY);
050         mMinute = c.get(Calendar.MINUTE);
051          
052         setDateTime();
053         setTimeOfDay();
054     }
055      
056     /**
057      * 初始化控件和UI视图
058      */
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);
064          
065         pickDate.setOnClickListener(new View.OnClickListener() {
066              
067             @Override
068             public void onClick(View v) {
069                Message msg = new Message();
070                if (pickDate.equals((Button) v)) { 
071                   msg.what = MainActivity.SHOW_DATAPICK; 
072                
073                MainActivity.this.dateandtimeHandler.sendMessage(msg);
074             }
075         });
076          
077         pickTime.setOnClickListener(new View.OnClickListener() {
078              
079             @Override
080             public void onClick(View v) {
081                Message msg = new Message();
082                if (pickTime.equals((Button) v)) { 
083                   msg.what = MainActivity.SHOW_TIMEPICK; 
084                
085                MainActivity.this.dateandtimeHandler.sendMessage(msg);
086             }
087         });
088     }
089  
090     /**
091      * 设置日期
092      */
093     private void setDateTime(){
094        final Calendar c = Calendar.getInstance(); 
095         
096        mYear = c.get(Calendar.YEAR); 
097        mMonth = c.get(Calendar.MONTH); 
098        mDay = c.get(Calendar.DAY_OF_MONTH);
099    
100        updateDateDisplay();
101     }
102      
103     /**
104      * 更新日期显示
105      */
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));
110     }
111      
112     /**
113      * 日期控件的事件
114      */ 
115     private DatePickerDialog.OnDateSetListener mDateSetListener = newDatePickerDialog.OnDateSetListener() { 
116    
117        public void onDateSet(DatePicker view, int year, int monthOfYear, 
118               int dayOfMonth) { 
119            mYear = year; 
120            mMonth = monthOfYear; 
121            mDay = dayOfMonth; 
122  
123            updateDateDisplay();
124        
125     };
126      
127     /**
128      * 设置时间
129      */
130     private void setTimeOfDay(){
131        final Calendar c = Calendar.getInstance();
132        mHour = c.get(Calendar.HOUR_OF_DAY);
133        mMinute = c.get(Calendar.MINUTE);
134        updateTimeDisplay();
135     }
136      
137     /**
138      * 更新时间显示
139      */
140     private void updateTimeDisplay(){
141        showTime.setText(new StringBuilder().append(mHour).append(":")
142                .append((mMinute < 10) ? "0" + mMinute : mMinute));
143     }
144      
145     /**
146      * 时间控件事件
147      */
148     private TimePickerDialog.OnTimeSetListener mTimeSetListener = newTimePickerDialog.OnTimeSetListener() {
149          
150         @Override
151         public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
152             mHour = hourOfDay;
153             mMinute = minute;
154              
155             updateTimeDisplay();
156         }
157     };
158      
159     @Override 
160     protected Dialog onCreateDialog(int id) { 
161        switch (id) { 
162        case DATE_DIALOG_ID: 
163            return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, 
164                   mDay);
165        case TIME_DIALOG_ID:
166            return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true);
167        }
168             
169        return null
170     
171    
172     @Override 
173     protected void onPrepareDialog(int id, Dialog dialog) { 
174        switch (id) { 
175        case DATE_DIALOG_ID: 
176            ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); 
177            break;
178        case TIME_DIALOG_ID:
179            ((TimePickerDialog) dialog).updateTime(mHour, mMinute);
180            break;
181        }
182     
183    
184     /**
185      * 处理日期和时间控件的Handler
186      */ 
187     Handler dateandtimeHandler = new Handler() {
188    
189        @Override 
190        public void handleMessage(Message msg) { 
191            switch (msg.what) { 
192            case MainActivity.SHOW_DATAPICK: 
193                showDialog(DATE_DIALOG_ID); 
194                break;
195            case MainActivity.SHOW_TIMEPICK:
196                showDialog(TIME_DIALOG_ID);
197                break;
198            
199        
200    
201     };
202      
203 }

【4】程序运行效果图:

Android中日期和时间控件的使用     Android中日期和时间控件的使用     Android中日期和时间控件的使用

转自:http://blog.csdn.net/cjjky/article/details/7292445
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值