Android 日期控件的简单实现


言语不多,直接看代码:main.xml文件代码

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:orientation="vertical" android:layout_width="fill_parent"  
  6.   
  7.     android:layout_height="fill_parent">  
  8.   
  9.     <TextView android:layout_width="fill_parent"  
  10.   
  11.        android:layout_height="wrap_content" android:text="日期控件的使用DEMO" />  
  12.   
  13.     <LinearLayout android:orientation="horizontal"  
  14.   
  15.        android:layout_width="fill_parent" android:layout_height="wrap_content">  
  16.   
  17.        <EditText android:id="@+id/showDate" android:layout_width="200dip"  
  18.   
  19.            android:layout_height="wrap_content" />  
  20.   
  21.        <Button android:id="@+id/but_showDate" android:layout_width="100dip"  
  22.   
  23.            android:layout_height="wrap_content" android:text="选择日期" />  
  24.   
  25.     </LinearLayout>  
  26.   
  27.    
  28.   
  29. </LinearLayout>  

MainActivity.java源代码:

[java]  view plain copy
  1. package com.wanghf.demo;  
  2.   
  3.    
  4.   
  5. import java.util.Calendar;  
  6.   
  7.    
  8.   
  9. import android.app.Activity;  
  10.   
  11. import android.app.DatePickerDialog;  
  12.   
  13. import android.app.Dialog;  
  14.   
  15. import android.os.Bundle;  
  16.   
  17. import android.os.Handler;  
  18.   
  19. import android.os.Message;  
  20.   
  21. import android.view.View;  
  22.   
  23. import android.widget.Button;  
  24.   
  25. import android.widget.DatePicker;  
  26.   
  27. import android.widget.EditText;  
  28.   
  29.    
  30.   
  31. /** 
  32.  
  33.  * 日期控件的简单使用 
  34.  
  35.  * 
  36.  
  37.  * @author adminelco 
  38.  
  39.  * @time 2011-07-20 15:42:24 
  40.  
  41.  * 
  42.  
  43.  */  
  44.   
  45. public class MainActivity extends Activity {  
  46.   
  47.     private EditText showDate = null;  
  48.   
  49.     private Button pickDate = null;  
  50.   
  51.     private static final int DATE_DIALOG_ID = 1;  
  52.   
  53.     private static final int SHOW_DATAPICK = 0;  
  54.   
  55.     private int mYear;  
  56.   
  57.     private int mMonth;  
  58.   
  59.     private int mDay;  
  60.   
  61.    
  62.   
  63.     /** Called when the activity is first created. */  
  64.   
  65.     @Override  
  66.   
  67.     public void onCreate(Bundle savedInstanceState) {  
  68.   
  69.        super.onCreate(savedInstanceState);  
  70.   
  71.        setContentView(R.layout.main);  
  72.   
  73.        showDate = (EditText) findViewById(R.id.showDate);  
  74.   
  75.        pickDate = (Button) findViewById(R.id.but_showDate);  
  76.   
  77.        pickDate.setOnClickListener(new DateButtonOnClickListener());  
  78.   
  79.        final Calendar c = Calendar.getInstance();  
  80.   
  81.        mYear = c.get(Calendar.YEAR);  
  82.   
  83.        mMonth = c.get(Calendar.MONTH);  
  84.   
  85.        mDay = c.get(Calendar.DAY_OF_MONTH);  
  86.   
  87.        setDateTime();  
  88.   
  89.     }  
  90.   
  91.    
  92.   
  93.     private void setDateTime() {  
  94.   
  95.        final Calendar c = Calendar.getInstance();  
  96.   
  97.        mYear = c.get(Calendar.YEAR);  
  98.   
  99.        mMonth = c.get(Calendar.MONTH);  
  100.   
  101.        mDay = c.get(Calendar.DAY_OF_MONTH);  
  102.   
  103.    
  104.   
  105.        updateDisplay();  
  106.   
  107.     }  
  108.   
  109.    
  110.   
  111.     /** 
  112.  
  113.      * 更新日期 
  114.  
  115.      */  
  116.   
  117.     private void updateDisplay() {  
  118.   
  119.        showDate.setText(new StringBuilder().append(mYear).append(  
  120.   
  121.               (mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append(  
  122.   
  123.               (mDay < 10) ? "0" + mDay : mDay));  
  124.   
  125.     }  
  126.   
  127.    
  128.   
  129.     /** 
  130.  
  131.      * 日期控件的事件 
  132.  
  133.      */  
  134.   
  135.     private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {  
  136.   
  137.        public void onDateSet(DatePicker view, int year, int monthOfYear,  
  138.   
  139.               int dayOfMonth) {  
  140.   
  141.            mYear = year;  
  142.   
  143.            mMonth = monthOfYear;  
  144.   
  145.            mDay = dayOfMonth;  
  146.   
  147.            updateDisplay();  
  148.   
  149.        }  
  150.   
  151.     };  
  152.   
  153.    
  154.   
  155.     /** 
  156.  
  157.      * 选择日期Button的事件处理 
  158.  
  159.      * 
  160.  
  161.      * @author Raul 
  162.  
  163.      * 
  164.  
  165.      */  
  166.   
  167.     class DateButtonOnClickListener implements  
  168.   
  169.            android.view.View.OnClickListener {  
  170.   
  171.        @Override  
  172.   
  173.        public void onClick(View v) {  
  174.   
  175.            Message msg = new Message();  
  176.   
  177.            if (pickDate.equals((Button) v)) {  
  178.   
  179.               msg.what = MainActivity.SHOW_DATAPICK;  
  180.   
  181.            }  
  182.   
  183.            MainActivity.this.saleHandler.sendMessage(msg);  
  184.   
  185.        }  
  186.   
  187.     }  
  188.   
  189.    
  190.   
  191.     @Override  
  192.   
  193.     protected Dialog onCreateDialog(int id) {  
  194.   
  195.        switch (id) {  
  196.   
  197.        case DATE_DIALOG_ID:  
  198.   
  199.            return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,  
  200.   
  201.                   mDay);  
  202.   
  203.        }  
  204.   
  205.        return null;  
  206.   
  207.     }  
  208.   
  209.    
  210.   
  211.     @Override  
  212.   
  213.     protected void onPrepareDialog(int id, Dialog dialog) {  
  214.   
  215.        switch (id) {  
  216.   
  217.        case DATE_DIALOG_ID:  
  218.   
  219.            ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);  
  220.   
  221.            break;  
  222.   
  223.        }  
  224.   
  225.     }  
  226.   
  227.    
  228.   
  229.     /** 
  230.  
  231.      * 处理日期控件的Handler 
  232.  
  233.      */  
  234.   
  235.     Handler saleHandler = new Handler() {  
  236.   
  237.        @Override  
  238.   
  239.        public void handleMessage(Message msg) {  
  240.   
  241.            switch (msg.what) {  
  242.   
  243.            case MainActivity.SHOW_DATAPICK:  
  244.   
  245.               showDialog(DATE_DIALOG_ID);  
  246.   
  247.               break;  
  248.   
  249.            }  
  250.   
  251.        }  
  252.   
  253.     };  
  254.   
  255. }  

显示效果:如图


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值