码迷,mamicode.com
首页 > 移动开发 > 详细

Android学习笔记之控件DatePicker和TimePicker

时间:2014-11-24 15:14:46      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   sp   java   

1.xml布局文件

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <TimePicker
 8         android:id="@+id/timePicker1"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content" />
11 
12     <DatePicker
13         android:id="@+id/datePicker1"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content" />
16 
17 </LinearLayout>

 

2.java代码

 

 1 public class MainActivity extends ActionBarActivity {
 2 
 3     private DatePicker dp1;
 4     private TimePicker tp1;
 5     private int year;
 6     private int month;
 7     private int day;
 8     private int hour;
 9     private int minute;
10     private Calendar cal;
11     
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.main);
16         //获取日历的一个对象
17         cal=Calendar.getInstance();
18         //获取年月日分秒的信息
19         year = cal.get(Calendar.YEAR);
20         month = cal.get(Calendar.MONTH)+1;
21         day = cal.get(Calendar.DAY_OF_MONTH);
22         hour = cal.get(Calendar.HOUR_OF_DAY);
23         minute = cal.get(Calendar.MINUTE);
24         
25         setTitle(year+"-"+month+"-"+day+" "+hour+":"+minute);
26         
27         //初始化
28         dp1 = (DatePicker) findViewById(R.id.datePicker1);
29         tp1 = (TimePicker) findViewById(R.id.timePicker1);
30         
31         //dp1初始化
32         //DatePicker的实现
33         dp1.init(year, cal.get(Calendar.MONTH), day, new OnDateChangedListener() {
34             
35             @Override
36             public void onDateChanged(DatePicker view, int year, int monthOfYear,
37                     int dayOfMonth) {
38                 
39                 setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
40             }
41         });
42         
43         //TimePicker的实现
44         tp1.setOnTimeChangedListener(new OnTimeChangedListener() {
45             
46             @Override
47             public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
48                 
49                 setTitle(hourOfDay+":"+minute);
50             }
51         });
52         
53         //DatePickerDialog的实现
54         DatePickerDialog dpd = new DatePickerDialog(this, new OnDateSetListener() {
55             
56             @Override
57             public void onDateSet(DatePicker view, int year, int monthOfYear,
58                     int dayOfMonth) {
59                 
60                 setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
61             }
62         }, year, cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
63         dpd.show();
64         
65         //TimePickerDialog的实现
66         TimePickerDialog tpd = new TimePickerDialog(this, new OnTimeSetListener() {
67             
68             @Override
69             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
70                 
71                 setTitle(hourOfDay+":"+minute);
72             }
73         }, hour, minute, true);
74         tpd.show();
75     }
76 }

3.关于月份,0代表1,所以直接用对象cal来获取月份信息

 

Android学习笔记之控件DatePicker和TimePicker

标签:android   style   blog   http   io   ar   color   sp   java   

原文地址:http://www.cnblogs.com/ShawDd/p/4118619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!