日期 时间 控制

引用:http://www.oschina.net/code/snippet_54100_5221

[代码] main.xml

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
03     Android:orientation="vertical"
04     Android:layout_width="fill_parent"
05     Android:layout_height="fill_parent"
06     >
07 <TextView  Android:id="@+id/dateAndTime"
08     Android:layout_width="fill_parent"
09     Android:layout_height="wrap_content"
10     Android:text="@string/hello"
11     />
12 <Button    Android:id="@+id/setDate"
13         Android:layout_width="fill_parent"
14         Android:layout_height="wrap_content"
15         Android:text="Set the Date"></Button>
16 <Button    Android:id="@+id/setTime"
17         Android:layout_width="fill_parent"
18         Android:layout_height="wrap_content"
19         Android:text="Set the Time"></Button>
20 </LinearLayout>

[代码] ChronoDemo.java

001 package yyl.Android;
002  
003 import java.text.DateFormat;
004 import java.util.Calendar;
005 import java.util.Locale;
006  
007 import Android.app.Activity;
008 import Android.app.DatePickerDialog;
009 import Android.app.TimePickerDialog;
010 import Android.os.Bundle;
011 import Android.view.View;
012 import Android.widget.Button;
013 import Android.widget.DatePicker;
014 import Android.widget.TextView;
015 import Android.widget.TimePicker;
016  
017 public class ChronoDemo extends Activity {
018    //获取日期格式器对象
019     DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
020     //定义一个TextView控件对象
021     TextView dateAndTimeLabel = null;
022     //获取一个日历对象
023     Calendar dateAndTime = Calendar.getInstance(Locale.CHINA);
024     
025     
026     //当点击DatePickerDialog控件的设置按钮时,调用该方法
027     DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener()
028     {
029         @Override
030         public void onDateSet(DatePicker view, int year, int monthOfYear,
031                 int dayOfMonth) {
032             //修改日历控件的年,月,日
033             //这里的year,monthOfYear,dayOfMonth的值与DatePickerDialog控件设置的最新值一致
034             dateAndTime.set(Calendar.YEAR, year);
035             dateAndTime.set(Calendar.MONTH, monthOfYear);
036             dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);  
037             //将页面TextView的显示更新为最新时间
038             updateLabel();          
039         }      
040     };
041     
042  
043  
044     TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
045         
046         //同DatePickerDialog控件
047         @Override
048         public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
049             dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
050             dateAndTime.set(Calendar.MINUTE, minute);
051             updateLabel();
052             
053         }
054     };
055     
056     @Override
057     public void onCreate(Bundle savedInstanceState) {
058         super.onCreate(savedInstanceState);
059         setContentView(R.layout.main);
060         
061         //得到页面设定日期的按钮控件对象
062         Button dateBtn = (Button)findViewById(R.id.setDate);
063         //设置按钮的点击事件监听器
064         dateBtn.setOnClickListener(new View.OnClickListener() {
065             
066             @Override
067             public void onClick(View v) {
068                 //生成一个DatePickerDialog对象,并显示。显示的DatePickerDialog控件可以选择年月日,并设置
069                 new DatePickerDialog(ChronoDemo.this,
070                         d,
071                         dateAndTime.get(Calendar.YEAR),
072                         dateAndTime.get(Calendar.MONTH),
073                         dateAndTime.get(Calendar.DAY_OF_MONTH)).show();              
074             }
075         });
076         
077         Button timeBtn = (Button)findViewById(R.id.setTime);
078         timeBtn.setOnClickListener(new View.OnClickListener() {
079             
080             //同上原理
081             @Override
082             public void onClick(View v) {
083                 new TimePickerDialog(ChronoDemo.this,
084                         t,
085                         dateAndTime.get(Calendar.HOUR_OF_DAY),
086                         dateAndTime.get(Calendar.MINUTE),
087                         true).show();
088                 
089             }
090         });
091         
092         dateAndTimeLabel=(TextView)findViewById(R.id.dateAndTime);
093         
094         updateLabel();
095     }
096     
097     //更新页面TextView的方法
098     private void updateLabel() {
099         dateAndTimeLabel.setText(fmtDateAndTime
100         .format(dateAndTime.getTime()));
101         }
102 }
posted @ 2011-09-23 15:45  镇水古月  阅读(207)  评论(0编辑  收藏  举报