DatePicker:日歷選擇器;
TimePicker:時間選擇器;
兩種形態:貼合頻幕,對話框(滾軸)形式出現;
兩組監聽器:
1.DatePicker與TimePicker中
日歷監聽器OnDateChangedListener(),
onDateChanged()事件;
時間監聽器OnTimerChangedListener(),
onTimeChanged()事件;
2.DatePickerDialog與TimePickerDialog中
日歷監聽器OnDateSetListener(),onDateSet()事件;
時間監聽器OnTimerSetListener(),onTimeset()事件;
日歷選擇器步驟:
第一步:新建xml文件,將日歷與時間拖入其中,用
setContentView()將xml與activity關聯;
第二步:新建DatePicker與TimePicker對象,新建Calendar對象顯示當前時間;
第三步:獲取日歷對象cal=Calendar.getInstance();獲取年月日時分秒信息,用int來存,year=cal.get(Calendar.YEAR)...;注意calenda的month是從0開始的,所以要加上1;用setTitle("")可以顯示到標題欄上;
第四步:先通過findViewById使布局與變量綁定到一起;調用成員函數init()初始化DatePicker,init(year,month,day,onDateChangedListener()),對onDateChangedListener(),一共有三種方式顯示出來;在onDateChanged()中調用setTitle()顯示到標題欄上;
時間選擇器:沒有init()初始化的操作,直接調用監聽器,將事件new出;
以對話框的形式實現:
new DatePickerDialog(context,callBack,year,monthOfYear,dayOfMonth).show():
context--this,上下文;
callBack--new OnDateSetListener()監聽器;
TimePickerDialog與DatePickerDialog相似;
setTitle:可以設置標題欄的內容;
要注意month是從0開始還是從1開始的;
2014-08-31
筆記記得好用心啊~
2014-11-22
很棒,借用一下了
2014-11-06
很詳細哦
2014-09-14
我也要這樣!學習一下!