android学习---时间日期控件

时间日期控件

DatePicker        用来实现日期输入设置

TimePicker        用来实现时间输入设置

DatePickerDialog     用来显示日期对话框

TimePickerDialog    用来显示时间对话框
 
AnalogClock        用来显示一个指针式时钟

DigitalClock      用来显示一个数字式时钟

 

 

DatePicker实现日期输入设置

常用方法:

public CalendarView getCalendarView();    //获取CalendarView

public boolean getCalendarViewShown();    //获取CalendarView是否显示

public int getDayOfMonth();          //获取当前日期的日 public long getMaxDate();           //获取最大日期 public long getMinDate();         //获取最小日期 public int getMonth();            //获取当前日期的月 public boolean getSpinnersShown();    //获取Spinners是否显示 public int getYear();             //获取当前日期的年 public void init(int year,int monthOfYear,int dayOfMonth,DatePicker.OnDateChangedListener onDateChangedListener);     //初始化日期 public void setCalendarViewShown(boolean shown);  //设置是否显示CalendarView public void setMaxDate(long maxDate);        //设置最大日期 public void setMinDate(long minDate);         //设置最小日期 public void setSpinnersShown(boolean shown);    //设置是否显示Spinners public void updateDate(int year,int month,int dayOfMonth);   //更新当前日期

 

TimePicker实现时间输入设置

常用方法:

public Integer getCurrentHour();         //获取当前时间的小时

public Integer getCurrentMinute();        //获取当前时间的分钟
 
public boolean is24HourView();           //获取是否为24小时模式

public void setCurrentHour(Integer currentHour);          //设置当前时间的小时

public void setCurrentMinute(Integer currentMinute);      //设置当前时间的分钟

public void setIs24HourView(Boolean is24HourView);        //设置24小时模式

 

DatePickerDialog日期对话框

常用方法:

public DatePicker getDatePicker();                                //获取DatePicker中的日期值 

public void onClick(DialogInterface dialog,int which);            //响应对话框中的点击事件

public void onDateChanged(DatePicker view,int year,int month,int day);       //响应日期改变事件

public void updateDate(int year,int monthOfYear,int dayOfMonth);             //更新当前日期

 

TimePickerDialog时间对话框

常用方法:

public void onClick(DialogInterface dialog,int which);                        //响应对话框中的点击事件

public void onTimeChanged(TimePicker view,int hourOfDay,int minute);          //响应时间改变事件

public void updateTime(int hourOfDay,int minuteOfHour);                       //更新当前时间

 

AnalogClock

  在Android中,AnalogClock用于显示指针式时钟,该时钟仅有时钟和分钟两个指针。

DigitalClock

  在Android中,DigitalClock用来显示数字式时钟,显示格式为HH:MM:SS AM/PM。

 

  1 package com.leaf.android;
  2 
  3 import java.text.SimpleDateFormat;
  4 import java.util.Calendar;
  5 
  6 import android.app.Activity;
  7 import android.app.DatePickerDialog;
  8 import android.app.DatePickerDialog.OnDateSetListener;
  9 import android.app.TimePickerDialog;
 10 import android.app.TimePickerDialog.OnTimeSetListener;
 11 import android.graphics.drawable.BitmapDrawable;
 12 import android.os.Bundle;
 13 import android.view.View;
 14 import android.view.View.OnClickListener;
 15 import android.widget.Button;
 16 import android.widget.DatePicker;
 17 import android.widget.TextView;
 18 import android.widget.DatePicker.OnDateChangedListener;
 19 import android.widget.TimePicker;
 20 import android.widget.TimePicker.OnTimeChangedListener;
 21 import android.widget.Toast;
 22 
 23 public class Main extends Activity implements OnDateChangedListener,
 24         OnTimeChangedListener, OnClickListener {
 25     /** Called when the activity is first created. */
 26 
 27     private TextView textView;
 28     private DatePicker datePicker;
 29     private TimePicker timePicker;
 30     private Button button1, button2;
 31     private int hourOfDay, minute;
 32     private int year, mouthOfYear, dayOfMouth;
 33 
 34     @Override
 35     public void onCreate(Bundle savedInstanceState) {
 36         super.onCreate(savedInstanceState);
 37         setContentView(R.layout.main);
 38         datePicker = (DatePicker) this.findViewById(R.id.datepicker);
 39         timePicker = (TimePicker) this.findViewById(R.id.timepicker);
 40         textView = (TextView) this.findViewById(R.id.textview);
 41         // minute = calendar.get(Calendar.MINUTE);// 获得当前的秒
 42         datePicker.init(2013, 1, 1, this);// 初始化日期
 43         timePicker.setIs24HourView(true);// 显示时间是否是按照24小时制
 44         timePicker.setOnTimeChangedListener(this);// 注册事件
 45         button1 = (Button) this.findViewById(R.id.button1);
 46         button2 = (Button) this.findViewById(R.id.button2);
 47         button1.setOnClickListener(this);
 48         button2.setOnClickListener(this);
 49         // 获得当前的时间,获得小时和分钟
 50         Calendar calendar = Calendar.getInstance();
 51         hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
 52         minute = calendar.get(calendar.MINUTE);
 53         year = calendar.get(calendar.YEAR);
 54         mouthOfYear = calendar.get(calendar.MONTH);
 55         dayOfMouth = calendar.get(calendar.DAY_OF_MONTH);
 56 
 57     }
 58 
 59     // 时间控件的触发
 60     public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
 61         // TODO Auto-generated method stub
 62         Toast.makeText(Main.this,
 63                 "hourOfDay:" + hourOfDay + "minute:" + minute, 1).show();
 64 
 65     }
 66 
 67     // 日期控件的触发
 68     public void onDateChanged(DatePicker view, int year, int monthOfYear,
 69             int dayOfMonth) {
 70         // TODO Auto-generated method stub
 71         Calendar calendar = Calendar.getInstance();
 72         calendar.set(datePicker.getYear(), datePicker.getMonth(),
 73                 datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
 74                 timePicker.getCurrentMinute());
 75         SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日   HH:mm");
 76         textView.setText(format.format(calendar.getTime()));
 77     }
 78 
 79     public void onClick(View v) {
 80         // TODO Auto-generated method stub
 81         switch (v.getId()) {
 82         case R.id.button1:
 83             DatePickerDialog datePickerDialog = new DatePickerDialog(Main.this,
 84                     new MyDatePickerDialog(), year, mouthOfYear, dayOfMouth);
 85             datePickerDialog.show();
 86             break;
 87         case R.id.button2:
 88             TimePickerDialog timePickerDialog = new TimePickerDialog(Main.this,
 89                     new MyTimePickerDialog(), hourOfDay, minute, true);
 90             timePickerDialog.show();
 91             break;
 92         }
 93     }
 94 
 95     public class MyDatePickerDialog implements OnDateSetListener {
 96 
 97         public void onDateSet(DatePicker view, int year, int monthOfYear,
 98                 int dayOfMonth) {
 99             // TODO Auto-generated method stub
100             Toast.makeText(
101                     Main.this,
102                     "year:" + year + "monthOfYear" + monthOfYear + "dayOfMonth"
103                             + dayOfMonth, 1).show();
104         }
105 
106     }
107 
108     public class MyTimePickerDialog implements OnTimeSetListener {
109 
110         public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
111             // TODO Auto-generated method stub
112             Toast.makeText(Main.this,
113                     "hourOfDay:" + hourOfDay + "minute:" + minute, 1).show();
114         }
115 
116     }
117 }
Main.java
 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:background="#FFFFFF"
 6     android:orientation="vertical" >
 7 
 8     <TextView
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:text="日期控件显示"
12         android:textColor="#FF0000" />
13 
14     <DatePicker
15         android:id="@+id/datepicker"
16         android:layout_width="fill_parent"
17         android:layout_height="wrap_content" />
18 
19     <TextView
20         android:layout_width="fill_parent"
21         android:layout_height="wrap_content"
22         android:text="时间控件显示"
23         android:textColor="#FF0000" />
24 
25     <TimePicker
26         android:id="@+id/timepicker"
27         android:layout_width="fill_parent"
28         android:layout_height="wrap_content" />
29 
30     <TextView
31         android:id="@+id/textview"
32         android:layout_width="fill_parent"
33         android:layout_height="wrap_content"
34         android:textSize="18dp" />
35 
36     <Button
37         android:id="@+id/button1"
38         android:layout_width="fill_parent"
39         android:layout_height="wrap_content"
40         android:text="显示DatePickerDialog" />
41 
42     <Button
43         android:id="@+id/button2"
44         android:layout_width="fill_parent"
45         android:layout_height="wrap_content"
46         android:text="显示TimePickerDialog" />
47 
48 </LinearLayout>
main.xml

效果:

    

posted on 2013-09-01 20:12  leafu  阅读(610)  评论(0编辑  收藏  举报

导航