首页 > 代码库 > Android学习笔记之控件DatePicker和TimePicker

Android学习笔记之控件DatePicker和TimePicker

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     <DatePicker13         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     @Override13     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             @Override36             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             @Override47             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             @Override57             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             @Override69             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