




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第Android開發實現日期時間控件選擇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
創建DateTimeDialog類是為了將方法封裝,以便我項目多次調用
publicclassDateTimeDialog{
}
2.1創建靜態方法
2.1.1創建SetDateDialog,用于選擇日期
publicstaticvoidSetDateDialog(Contextcontext,Activityactivity,TextViewtextView,String...title){
}
這里我們將引用控件的context、activity作為參數傳入方法中,方便我們動態加載Layout和指定AlertDialog彈出所在的Activity,避免彈出框無法顯示。
textView參數為需要綁定選擇的控件,并且在選擇之后,會將選擇的日期返回給textView
titile是可選參數,指定彈出框的標題,不指定的話,會默認為選擇日期
2.1.2SetDateDialog中綁定textView的click事件
給textView綁定事件后,在用戶點擊控件時即可執行相應的事件內容,在此我們需要的是用戶點擊控件時,彈出日期選擇框。
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);
設置彈出框的標題
if(title!=nulltitle.length0){
builder.setTitle(title[0]);
}else
builder.setTitle("選擇日期");
實現彈出框的按鈕事件:
點擊確定時,綁定值給textView,并關閉彈窗;點擊取消時,直接關閉天窗;點擊現在時,將當前時間傳給textView,并關閉彈窗。
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("現在",(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);
//設置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("現在",(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();
});
}
同樣的方式我們再實現選擇日期時間的方法,具體不再贅述,上代碼:
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));
//設置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("現在",(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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 營養與骨骼健康促進考核試卷
- 農藥生產過程有害生物防治技術考核試卷
- 保護大自然的演講稿
- 企業半年工作總結(集合14篇)
- 個人周工作總結3篇
- 河南省重大活動方案
- 正月初六回娘家活動方案
- 正月店鋪活動策劃方案
- 水果相關活動方案
- 汽車集團活動方案
- 2024年雞西市公安局招聘警務輔助人員筆試真題
- 2023年度內蒙古自治區政府采購評審專家資格考試題庫
- 國家開放大學法律事務專科《法理學》期末紙質考試第一大題單項選擇題庫2025春期考試版
- 企業道路交通安全宣傳
- 635MPa級熱軋帶肋高強鋼筋應用技術規程
- 人教版語文四年級上冊全冊知識點
- 公對公咨詢居間協議書范本
- 隧道鋼拱架加工
- 國開電大《鋼結構(本)》階段性學習測驗1-4
- 大學《麻醉學》期末復習重點及習題解析(名詞解釋、選擇、填空、簡答、病例分析)
- 他汀不耐受的臨床診斷與處理中國專家共識(2024)解讀課件
評論
0/150
提交評論