一、事先準備:java
熟悉DatePicker和TimePicker兩種實現動態輸入日期和時間的功能,輸入模塊貼合在屏幕上;android
熟悉DatePickerDialog和TimePickerDialog兩種實現動態輸入日期和時間的對話框,輸入模塊以對話框形式出現;api
二、針對檢測日期時間改變的監聽器:app
a、針對輸入模塊貼合在屏幕的監聽器:OnDateChangedListener和OnTimeChangedListener()ide
當用戶更改DataPicker裏的年月日時,將觸發OnDateChangedListener監聽器的onDateChange()事件;佈局
當用戶更改了TimePicker裏的時分時,將觸發OnTimeChangedListener()的onTimeChanged()事件;this
b、針對輸入模塊以對話框形式出現的監聽器:OnDateSetListener和OnTimeSetListener()code
當用戶更改DataPickerDialog裏的年月日時,將觸發OnDateSetListener監聽器的onDateSet()事件;xml
當用戶更改TimePickerDialog裏的時分時,將觸發OnTimeSetListener監聽器的onTimeSet()事件;對象
三、樣例需求功能描述:
默認在標題欄中展現當前日期和時間,經過監聽器去獲取用戶選擇日期或時間的結果,並將變動的日期或時間展現在標題欄上。
利用setTitle去改變標題欄內容。
四、使用DataPicker實現日曆選擇器,使用TimePicker實現時間選擇器:
建立main.xml文件,設置其佈局爲:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
修改MainActivity.java文件
package com.example.myandroiddatapickerandtimepicker; import java.util.Calendar; import android.os.Bundle; import android.R.integer; import android.app.Activity; import android.view.Menu; import android.widget.CalendarView; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MainActivity extends Activity { //定義 private TimePicker timePicker; private DatePicker datePicker; private Calendar cal; private int year; private int month; private int day; private int hour; private int minute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取日曆的對象 cal=Calendar.getInstance(); //獲取年月日時分秒的信息 year=cal.get(Calendar.YEAR); month=cal.get(Calendar.MONTH)+1;//從0開始計算月份 day=cal.get(Calendar.DAY_OF_MONTH); hour=cal.get(Calendar.HOUR_OF_DAY); minute=cal.get(Calendar.MINUTE); //顯示在標題欄 setTitle(year+"-"+month+"-"+day+"-"+hour+":"+minute); //用findById方法將佈局文件和變量進行綁定 datePicker=(DatePicker) findViewById(R.id.datePicker); timePicker=(TimePicker) findViewById(R.id.timePicker); //datePicker初始化 datePicker.init(year, cal.get(Calendar.MONTH), day, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayofMonth) { // TODO Auto-generated method stub setTitle(year+"-"+monthOfYear+1+"-"+dayofMonth); } }); timePicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub setTitle(hourOfDay+":"+minute); } }); } }
這樣選擇的日期、時間將動態刷新到標題欄
五、使用DataPickerDialog實現日曆選擇器,使用TimePickerDialog實現時間選擇器:
修改上述MainActivity.java文件
package com.example.myandroiddatapickerandtimepicker; import java.util.Calendar; import android.os.Bundle; import android.R.integer; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.view.Menu; import android.widget.CalendarView; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MainActivity extends Activity { //定義 private TimePicker timePicker; private DatePicker datePicker; private Calendar cal; private int year; private int month; private int day; private int hour; private int minute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取日曆的對象 cal=Calendar.getInstance(); //獲取年月日時分秒的信息 year=cal.get(Calendar.YEAR); month=cal.get(Calendar.MONTH)+1;//從0開始計算月份 day=cal.get(Calendar.DAY_OF_MONTH); hour=cal.get(Calendar.HOUR_OF_DAY); minute=cal.get(Calendar.MINUTE); //顯示在標題欄 setTitle(year+"-"+month+"-"+day+"-"+hour+":"+minute); //DatePickerDialog的初始化 /*new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); } }, year, cal.get(Calendar.MONTH), day).show();*/ new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub setTitle(hourOfDay+":"+minute); } }, hour, minute, true).show(); } }