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

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

分类: Android 进阶   1647人阅读  评论(1)  收藏  举报

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

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

【1】Demo程序框架图:

【2】布局文件 res/layout/main.xml 源码:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <TextView   
  7.        android:layout_width="fill_parent" android:layout_height="wrap_content"   
  8.        android:gravity="center" android:text="欢迎关注Andy.Chen Blog" />   
  9.     <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源码:

[html]  view plain copy
  1. package com.andyidea.calenderdemo;  
  2.   
  3. import java.util.Calendar;  
  4.   
  5. import android.app.Activity;  
  6. import android.app.DatePickerDialog;  
  7. import android.app.Dialog;  
  8. import android.app.TimePickerDialog;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.os.Message;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.DatePicker;  
  15. import android.widget.EditText;  
  16. import android.widget.TimePicker;  
  17.   
  18. public class MainActivity extends Activity {  
  19.       
  20.     private EditText showDate = null;  
  21.     private Button pickDate = null;  
  22.     private EditText showTime = null;  
  23.     private Button pickTime = null;  
  24.       
  25.     private static final int SHOW_DATAPICK = 0;   
  26.     private static final int DATE_DIALOG_ID = 1;    
  27.     private static final int SHOW_TIMEPICK = 2;  
  28.     private static final int TIME_DIALOG_ID = 3;  
  29.       
  30.     private int mYear;    
  31.     private int mMonth;  
  32.     private int mDay;   
  33.     private int mHour;  
  34.     private int mMinute;  
  35.       
  36.     /** Called when the activity is first created. */  
  37.     @Override  
  38.     public void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.main);  
  41.           
  42.         initializeViews();  
  43.           
  44.         final Calendar c = Calendar.getInstance();  
  45.         mYear = c.get(Calendar.YEAR);    
  46.         mMonth = c.get(Calendar.MONTH);    
  47.         mDay = c.get(Calendar.DAY_OF_MONTH);  
  48.           
  49.         mHour = c.get(Calendar.HOUR_OF_DAY);  
  50.         mMinute = c.get(Calendar.MINUTE);  
  51.           
  52.         setDateTime();   
  53.         setTimeOfDay();  
  54.     }  
  55.       
  56.     /**  
  57.      * 初始化控件和UI视图  
  58.      */  
  59.     private void initializeViews(){  
  60.         showDate = (EditText) findViewById(R.id.showdate);    
  61.         pickDate = (Button) findViewById(R.id.pickdate);   
  62.         showTime = (EditText)findViewById(R.id.showtime);  
  63.         pickTime = (Button)findViewById(R.id.picktime);  
  64.           
  65.         pickDate.setOnClickListener(new View.OnClickListener() {  
  66.               
  67.             @Override  
  68.             public void onClick(View v) {  
  69.                Message msg = new Message();   
  70.                if (pickDate.equals((Button) v)) {    
  71.                   msg.what = MainActivity.SHOW_DATAPICK;    
  72.                }    
  73.                MainActivity.this.dateandtimeHandler.sendMessage(msg);   
  74.             }  
  75.         });  
  76.           
  77.         pickTime.setOnClickListener(new View.OnClickListener() {  
  78.               
  79.             @Override  
  80.             public void onClick(View v) {  
  81.                Message msg = new Message();   
  82.                if (pickTime.equals((Button) v)) {    
  83.                   msg.what = MainActivity.SHOW_TIMEPICK;    
  84.                }    
  85.                MainActivity.this.dateandtimeHandler.sendMessage(msg);   
  86.             }  
  87.         });  
  88.     }  
  89.   
  90.     /**  
  91.      * 设置日期  
  92.      */  
  93.     private void setDateTime(){  
  94.        final Calendar c = Calendar.getInstance();    
  95.          
  96.        mYear = c.get(Calendar.YEAR);    
  97.        mMonth = c.get(Calendar.MONTH);    
  98.        mDay = c.get(Calendar.DAY_OF_MONTH);   
  99.     
  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 = new DatePickerDialog.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 = new TimePickerDialog.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】程序运行效果图:

       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值