




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第Android開發(fā)實現(xiàn)日期時間控件選擇1.2Layout中添加日期和時間控件
注意需要將控件的calendarViewShown指定為false及datePickerMode屬性指定為spinner
xmlversion="1.0"encoding="utf-8"
LinearLayoutxmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
DatePicker
android:id="@+id/dialog_datetime_date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"/
TimePicker
android:id="@+id/dialog_datetime_time_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"/
/LinearLayout
二、新建DateTimeDialog
創(chuàng)建DateTimeDialog類是為了將方法封裝,以便我項目多次調(diào)用
publicclassDateTimeDialog{
}
2.1創(chuàng)建靜態(tài)方法
2.1.1創(chuàng)建SetDateDialog,用于選擇日期
publicstaticvoidSetDateDialog(Contextcontext,Activityactivity,TextViewtextView,String...title){
}
這里我們將引用控件的context、activity作為參數(shù)傳入方法中,方便我們動態(tài)加載Layout和指定AlertDialog彈出所在的Activity,避免彈出框無法顯示。
textView參數(shù)為需要綁定選擇的控件,并且在選擇之后,會將選擇的日期返回給textView
titile是可選參數(shù),指定彈出框的標題,不指定的話,會默認為選擇日期
2.1.2SetDateDialog中綁定textView的click事件
給textView綁定事件后,在用戶點擊控件時即可執(zhí)行相應的事件內(nèi)容,在此我們需要的是用戶點擊控件時,彈出日期選擇框。
textView.setOnClickListener(view-{
AlertDialog.Builderbuilder=newAlertDialog.Builder(activity);
builder.setView(view1);
builder.create().show();
});
由于我們的控件中含有時間控件,需要隱藏
Viewview1=LayoutInflater.from(context).inflate(R.layout.dialog_datetime,null);
finalTimePickertimePicker=view1.findViewById(R.id.dialog_datetime_time_picker);
timePicker.setVisibility(View.GONE);
如果textView有默認值,則在彈出的時候需要將textView的日期帶入彈出框中
finalDatePickerdatePicker=view1.findViewById(R.id.dialog_datetime_date_picker);
Calendarcalendar;
StringstrDate=textView.getText().toString();
calendar=convertDateToCalendar(strDate);
datePicker.init(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE),null);
設(shè)置彈出框的標題
if(title!=nulltitle.length0){
builder.setTitle(title[0]);
}else
builder.setTitle("選擇日期");
實現(xiàn)彈出框的按鈕事件:
點擊確定時,綁定值給textView,并關(guān)閉彈窗;點擊取消時,直接關(guān)閉天窗;點擊現(xiàn)在時,將當前時間傳給textView,并關(guān)閉彈窗。
builder.setPositiveButton("確定",(dialog,i)-{
//日期格式
intyear=datePicker.getYear();
intmonth=datePicker.getMonth()+1;
intdayOfMonth=datePicker.getDayOfMonth();
textView.setText(String.format(Locale.getDefault(),"%d年%d月%d日",year,month,dayOfMonth));
dialog.cancel();
});
builder.setNegativeButton("取消",(dialog,which)-dialog.cancel());
builder.setNeutralButton("現(xiàn)在",(dialog,i)-{
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat("yyyy年M月d日",Locale.getDefault());//HH:mm:ss
//獲取當前時間
Datedate=newDate(System.currentTimeMillis());
textView.setText(simpleDateFormat.format(date));
dialog.cancel();
});
以下是完整代碼:
publicstaticvoidSetDateDialog(Contextcontext,Activityactivity,TextViewtextView,String...title){
textView.setOnClickListener(view-{
AlertDialog.Builderbuilder=newAlertDialog.Builder(activity);
Viewview1=LayoutInflater.from(context).inflate(R.layout.dialog_datetime,null);
finalDatePickerdatePicker=view1.findViewById(R.id.dialog_datetime_date_picker);
finalTimePickertimePicker=view1.findViewById(R.id.dialog_datetime_time_picker);
timePicker.setIs24HourView(true);
Calendarcalendar;
StringstrDate=textView.getText().toString();
calendar=convertDateToCalendar(strDate);
datePicker.init(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE),null);
timePicker.setHour(calendar.get(Calendar.HOUR_OF_DAY));
timePicker.setMinute(calendar.get(Calendar.MINUTE));
timePicker.setVisibility(View.GONE);
//
datePicker.setCalendarViewShown(false);
//設(shè)置Date布局
builder.setView(view1);
if(title!=nulltitle.length0){
builder.setTitle(title[0]);
}else
builder.setTitle("選擇日期");
builder.setPositiveButton("確定",(dialog,i)-{
//日期格式
intyear=datePicker.getYear();
intmonth=datePicker.getMonth()+1;
intdayOfMonth=datePicker.getDayOfMonth();
textView.setText(String.format(Locale.getDefault(),"%d年%d月%d日",year,month,dayOfMonth));
dialog.cancel();
});
builder.setNegativeButton("取消",(dialog,which)-dialog.cancel());
builder.setNeutralButton("現(xiàn)在",(dialog,i)-{
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat("yyyy年M月d日",Locale.getDefault());//HH:mm:ss
//獲取當前時間
Datedate=newDate(System.currentTimeMillis());
textView.setText(simpleDateFormat.format(date));
dialog.cancel();
});
builder.create().show();
});
}
同樣的方式我們再實現(xiàn)選擇日期時間的方法,具體不再贅述,上代碼:
publicstaticvoidSetDateTimeDialog(Contextcontext,Activityactivity,TextViewtextView,String...title){
textView.setOnClickListener(view-{
AlertDialog.Builderbuilder=newAlertDialog.Builder(activity);
Viewview1=LayoutInflater.from(context).inflate(R.layout.dialog_datetime,null);
finalDatePickerdatePicker=view1.findViewById(R.id.dialog_datetime_date_picker);
finalTimePickertimePicker=view1.findViewById(R.id.dialog_datetime_time_picker);
timePicker.setIs24HourView(true);
//
datePicker.setCalendarViewShown(false);
Calendarcalendar;
StringstrDate=textView.getText().toString();
calendar=convertDateToCalendar(strDate);
datePicker.init(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE),null);
timePicker.setHour(calendar.get(Calendar.HOUR_OF_DAY));
timePicker.setMinute(calendar.get(Calendar.MINUTE));
//設(shè)置Date布局
builder.setView(view1);
if(title!=nulltitle.length0){
builder.setTitle(title[0]);
}else
builder.setTitle("選擇時間");
builder.setPositiveButton("確定",(dialog,i)-{
//日期格式
intyear=datePicker.getYear();
intmonth=datePicker.getMonth()+1;
intdayOfMonth=datePicker.getDayOfMonth();
inthour=timePicker.getHour();
intmin=timePicker.getMinute();
//
timePicker.getSecond();
textView.setText(String.format(Locale.getDefault(),"%d年%d月%d日%d:%d",year,month,dayOfMonth,hour,min));
dialog.cancel();
});
builder.setNegativeButton("取消",(dialog,which)-dialog.cancel());
builder.setNeutralButton("現(xiàn)在",(dialog,i)-{
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat("yyyy年M月d日HH:mm",Locale.getDefault());//HH:mm:ss
//獲取當前時間
Datedate=newDate(System.currentTimeMillis());
textView.setText(simpleDateFormat.format(date));
dialog.cancel();
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 客車檢車員高級工考試題(含答案解析)
- 磁共振醫(yī)師三基練習題庫含參考答案解析
- 石灰在環(huán)保脫硫技術(shù)中的應用考核試卷
- 營養(yǎng)補充劑批發(fā)商的智慧供應鏈建設(shè)路徑考核試卷
- 環(huán)境監(jiān)測與突發(fā)事件應對考核試卷
- 草本植物在保健食品中的應用考核試卷
- 草原動物疫病防控監(jiān)測考核試卷
- 深海油氣資源開發(fā)環(huán)境保護措施及其對開發(fā)工程的影響考核試卷
- 砼結(jié)構(gòu)施工圖識讀與應用考核試卷
- 體育運動對健康促進的作用考核試卷
- 兒童行為干預效果評估的機器學習方法-洞察闡釋
- 區(qū)塊鏈考試試題及答案
- 2025-2030中國氟化工行業(yè)市場發(fā)展現(xiàn)狀及發(fā)展趨勢與投資前景研究報告
- 2025年保密觀知識競賽題庫附答案(黃金題型)含答案詳解
- 2024年呼和浩特市玉泉區(qū)消防救援大隊招聘真題
- 2025年山東省青島市萊西市中考一模英語試題(原卷版+解析版)
- SL631水利水電工程單元工程施工質(zhì)量驗收標準第3部分:地基處理與基礎(chǔ)工程
- 新22J01 工程做法圖集
- 2024年山東省濟南市中考英語試題卷(含答案解析)
- 中國陶瓷欣賞智慧樹知到期末考試答案章節(jié)答案2024年中國地質(zhì)大學(武漢)
- 2019年一級注冊消防工程師繼續(xù)教育三科題庫+答案
評論
0/150
提交評論