Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android實現日期時間選擇器(DatePicker和TimePicker) 示例

Android實現日期時間選擇器(DatePicker和TimePicker) 示例

編輯:Android開發實例

利用Android應用框架提供的DatePicker(日期選擇器)和TimePicker(時間選擇器),實現日期時間選擇器。

Dialog的Content布局文件(date_time_dialog.xml):

  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.     android:padding="10dip" > 
  7.  
  8.     <TextView 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="wrap_content" 
  11.         android:singleLine="true" 
  12.         android:text="請選擇日期" 
  13.         android:textColor="#FFFFFF" 
  14.         android:textSize="16sp" /> 
  15.  
  16.     <DatePicker 
  17.         android:id="@+id/date_picker" 
  18.         android:layout_width="wrap_content" 
  19.         android:layout_height="wrap_content" 
  20.         android:layout_marginTop="5dip" /> 
  21.  
  22.     <TextView 
  23.         android:layout_width="fill_parent" 
  24.         android:layout_height="wrap_content" 
  25.         android:layout_marginTop="10dip" 
  26.         android:singleLine="true" 
  27.         android:text="請選擇時間" 
  28.         android:textColor="#FFFFFF" 
  29.         android:textSize="16sp" /> 
  30.  
  31.     <TimePicker 
  32.         android:id="@+id/time_picker" 
  33.         android:layout_width="wrap_content" 
  34.         android:layout_height="wrap_content" 
  35.         android:layout_marginTop="5dip" /> 
  36.  
  37. </LinearLayout> 

代碼中的實現:

  1. package com.easipass.test;  
  2.  
  3. import java.util.Calendar;  
  4.  
  5. import android.app.Activity;  
  6. import android.app.AlertDialog;  
  7. import android.app.Dialog;  
  8. import android.content.DialogInterface;  
  9. import android.os.Bundle;  
  10. import android.text.InputType;  
  11. import android.view.MotionEvent;  
  12. import android.view.View;  
  13. import android.widget.DatePicker;  
  14. import android.widget.EditText;  
  15. import android.widget.TimePicker;  
  16.  
  17. /**  
  18.  * 功能描述:實現日期時間選擇器  
  19.  *   
  20.  * @author android_ls  
  21.  */ 
  22. public class DateTimeActivity extends Activity implements View.OnTouchListener {  
  23.      
  24.     private EditText etStartTime;  
  25.  
  26.     private EditText etEndTime;  
  27.       
  28.     @Override 
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.           
  33.         etStartTime = (EditText) this.findViewById(R.id.et_start_time);  
  34.         etEndTime = (EditText) this.findViewById(R.id.et_end_time);  
  35.           
  36.         etStartTime.setOnTouchListener(this);  
  37.         etEndTime.setOnTouchListener(this);  
  38.           
  39.     }  
  40.  
  41.     @Override 
  42.     public boolean onTouch(View v, MotionEvent event) {  
  43.         if (event.getAction() == MotionEvent.ACTION_DOWN) {  
  44.  
  45.             AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  46.             View view = View.inflate(this, R.layout.date_time_dialog, null);  
  47.             final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);  
  48.             final TimePicker timePicker = (android.widget.TimePicker) view.findViewById(R.id.time_picker);  
  49.             builder.setView(view);  
  50.  
  51.             Calendar cal = Calendar.getInstance();  
  52.             cal.setTimeInMillis(System.currentTimeMillis());  
  53.             datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);  
  54.  
  55.             timePicker.setIs24HourView(true);  
  56.             timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));  
  57.             timePicker.setCurrentMinute(Calendar.MINUTE);  
  58.  
  59.             if (v.getId() == R.id.et_start_time) {  
  60.                 final int inType = etStartTime.getInputType();  
  61.                 etStartTime.setInputType(InputType.TYPE_NULL);  
  62.                 etStartTime.onTouchEvent(event);  
  63.                 etStartTime.setInputType(inType);  
  64.                 etStartTime.setSelection(etStartTime.getText().length());  
  65.                   
  66.                 builder.setTitle("選取起始時間");  
  67.                 builder.setPositiveButton("確  定", new DialogInterface.OnClickListener() {  
  68.  
  69.                     @Override 
  70.                     public void onClick(DialogInterface dialog, int which) {  
  71.  
  72.                         StringBuffer sb = new StringBuffer();  
  73.                         sb.append(String.format("%d-%02d-%02d",   
  74.                                 datePicker.getYear(),   
  75.                                 datePicker.getMonth() + 1,  
  76.                                 datePicker.getDayOfMonth()));  
  77.                         sb.append("  ");  
  78.                         sb.append(timePicker.getCurrentHour())  
  79.                         .append(":").append(timePicker.getCurrentMinute());  
  80.  
  81.                         etStartTime.setText(sb);  
  82.                         etEndTime.requestFocus();  
  83.                           
  84.                         dialog.cancel();  
  85.                     }  
  86.                 });  
  87.                   
  88.             } else if (v.getId() == R.id.et_end_time) {  
  89.                 int inType = etEndTime.getInputType();  
  90.                 etEndTime.setInputType(InputType.TYPE_NULL);      
  91.                 etEndTime.onTouchEvent(event);  
  92.                 etEndTime.setInputType(inType);  
  93.                 etEndTime.setSelection(etEndTime.getText().length());  
  94.  
  95.                 builder.setTitle("選取結束時間");  
  96.                 builder.setPositiveButton("確  定", new DialogInterface.OnClickListener() {  
  97.  
  98.                     @Override 
  99.                     public void onClick(DialogInterface dialog, int which) {  
  100.  
  101.                         StringBuffer sb = new StringBuffer();  
  102.                         sb.append(String.format("%d-%02d-%02d",   
  103.                                 datePicker.getYear(),   
  104.                                 datePicker.getMonth() + 1,   
  105.                                 datePicker.getDayOfMonth()));  
  106.                         sb.append("  ");  
  107.                         sb.append(timePicker.getCurrentHour())  
  108.                         .append(":").append(timePicker.getCurrentMinute());  
  109.                         etEndTime.setText(sb);  
  110.                           
  111.                         dialog.cancel();  
  112.                     }  
  113.                 });  
  114.             }  
  115.               
  116.             Dialog dialog = builder.create();  
  117.             dialog.show();  
  118.         }  
  119.  
  120.         return true;  
  121.     }  
  122.       
  123. }  

運行後的效果圖:

 

點擊EditTet之後

點擊確定之後


 

 

 轉自:http://blog.csdn.net/android_ls/article/details/8644067

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved