android 日期选择器(DatePicker)学习与应用

文章转自:http://llhdf.javaeye.com/blog/521272

 

 

[c-sharp] view plaincopy
  1. import java.util.Calendar;     
  2. import android.app.Activity;     
  3. import android.app.DatePickerDialog;     
  4. import android.app.Dialog;     
  5. import android.os.Bundle;     
  6. import android.view.View;     
  7. import android.widget.Button;     
  8. import android.widget.DatePicker;     
  9. import android.widget.TextView;     
  10.     
  11. public class Datepicker extends Activity {     
  12.          
  13.     private TextView mDateDisplay;     
  14.     private Button mPickDate;     
  15.     
  16.     private int mYear;     
  17.     private int mMonth;     
  18.     private int mDay;     
  19.     
  20.     static final int DATE_DIALOG_ID = 0;     
  21.     
  22.     @Override    
  23.     protected void onCreate(Bundle savedInstanceState) {     
  24.         super.onCreate(savedInstanceState);     
  25.         setContentView(R.layout.main);     
  26.     
  27.       
  28.         mDateDisplay = (TextView) findViewById(R.id.dateDisplay);     
  29.         mPickDate = (Button) findViewById(R.id.pickDate);     
  30.     
  31.           
  32.         mPickDate.setOnClickListener(new View.OnClickListener() {     
  33.             public void onClick(View v) {     
  34.                 showDialog(DATE_DIALOG_ID);     
  35.             }     
  36.         });     
  37.     
  38.         //获得当前时间  
  39.         final Calendar c = Calendar.getInstance();     
  40.         mYear = c.get(Calendar.YEAR);     
  41.         mMonth = c.get(Calendar.MONTH);     
  42.         mDay = c.get(Calendar.DAY_OF_MONTH);     
  43.     
  44.         //显示当前时间  
  45.         updateDisplay();     
  46.     }     
  47.          
  48.     @Override    
  49.     protected Dialog onCreateDialog(int id) {     
  50.         switch (id) {     
  51.         case DATE_DIALOG_ID:     
  52.             return new DatePickerDialog(this,     
  53.                         mDateSetListener,     
  54.                         mYear, mMonth, mDay);     
  55.         }     
  56.         return null;     
  57.     }     
  58.          
  59.     // updates the date we display in the TextView     
  60.     private void updateDisplay() {     
  61.         mDateDisplay.setText(     
  62.             new StringBuilder()     
  63.                     // Month is 0 based so add 1     
  64.                     .append(mMonth + 1).append("-")     
  65.                     .append(mDay).append("-")     
  66.                     .append(mYear).append(" "));     
  67.     }     
  68.     
  69.    
  70.     private DatePickerDialog.OnDateSetListener mDateSetListener =     
  71.         new DatePickerDialog.OnDateSetListener() {     
  72.     
  73.             public void onDateSet(DatePicker view, int year,      
  74.                                   int monthOfYear, int dayOfMonth) {     
  75.                 mYear = year;     
  76.                 mMonth = monthOfYear;     
  77.                 mDay = dayOfMonth;     
  78.                 updateDisplay();     
  79.             }     
  80.     };     
  81.          
  82. }    

 

 

 

[c-sharp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="wrap_content"    
  4.     android:layout_height="wrap_content"    
  5.     android:orientation="vertical">    
  6.     
  7.     <TextView android:id="@+id/dateDisplay"    
  8.             android:layout_width="wrap_content"    
  9.             android:layout_height="wrap_content"    
  10.             android:text=""/>    
  11.     
  12.     <Button android:id="@+id/pickDate"    
  13.             android:layout_width="wrap_content"    
  14.             android:layout_height="wrap_content"    
  15.             android:text="设  置"/>    
  16.     
  17. </LinearLayout>    

 

 

小结:

主要是调用了Android里的DatePicker控件,

并设置了一个按钮、一个文本框用于展示效果。

没有难度,只需控制好相应的事件

posted @ 2012-06-28 17:31  郑文亮  阅读(4162)  评论(0编辑  收藏  举报