Android日期和時間選擇器實現(xiàn)代碼
更新時間:2017年11月10日 10:18:10 作者:Glydi
這篇文章主要為大家詳細介紹了Android日期和時間選擇器實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
抽出來了一個方法來選擇時間(這里自己規(guī)定的只能選擇當(dāng)前時間以后的日期),日期選擇完畢就會自動彈出時間選擇器讓選擇時間。
/**
* 選擇日期和時間
*/
private void selectDataAndTime() {
// 獲取當(dāng)前時間
final Calendar calendar = Calendar.getInstance();
/*
* toast("當(dāng)前時間是:" + calendar.get(Calendar.YEAR) + "," +
* calendar.get(Calendar.MONTH) + "," +
* calendar.get(Calendar.DAY_OF_MONTH));
*/
// 日期選擇對話框
dataPickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// 判斷用戶選擇的日期是否合法
if (calendar.get(Calendar.YEAR) > year) {
toast("時間有誤,請從新選擇");
return;
} else if (calendar.get(Calendar.YEAR) == year) {
if (calendar.get(Calendar.MONTH) > month) {
toast("時間有誤,請從新選擇");
return;
} else if (calendar.get(Calendar.MONTH) == month) {
if (calendar.get(Calendar.DAY_OF_MONTH) > day) {
toast("時間有誤,請從新選擇");
return;
} else {
strDate = year + "-" + (month + 1) + "-" + day;
if (timePickerDialog != null) {
timePickerDialog.show();
}
}
} else {
strDate = year + "-" + (month + 1) + "-" + day;
if (timePickerDialog != null) {
timePickerDialog.show();
}
}
} else {
strDate = year + "-" + (month + 1) + "-" + day;
if (timePickerDialog != null) {
timePickerDialog.show();
}
}
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar
.get(Calendar.DAY_OF_MONTH));
// 時間選擇對話框
timePickerDialog = new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
strTime = strDate + " " + hour + ":" + minute;
timeTt.setText(strTime);
}
}, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), true);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android自定義DataTimePicker實例代碼(日期選擇器)
- Android中的TimePickerView(時間選擇器)的用法詳解
- Android?studio實現(xiàn)日期?、時間選擇器與進度條
- Android仿IOS10圓盤時間選擇器
- Android仿iPhone日期時間選擇器詳解
- Android Studio時間選擇器的創(chuàng)建方法
- Android自定義View仿IOS圓盤時間選擇器
- Android開發(fā)中實現(xiàn)IOS風(fēng)格底部選擇器(支持時間 日期 自定義)
- Android時間選擇器、日期選擇器實現(xiàn)代碼
- Android自定義DataTimePicker日期時間選擇器使用詳解
相關(guān)文章
淺談Android獲取ImageView上的圖片,和一個有可能遇到的問題
下面小編就為大家?guī)硪黄獪\談Android獲取ImageView上的圖片,和一個有可能遇到的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
淺談onTouch先執(zhí)行,還是onClick執(zhí)行(詳解)
onTouch先執(zhí)行,還是onClick執(zhí)行?下面小編就為大家?guī)硪黄獪\談onTouch先執(zhí)行,還是onClick執(zhí)行(詳解)。希望對大家有所幫助。一起跟隨小編過來看看吧2017-03-03
android仿微信通訊錄搜索示例(匹配拼音,字母,索引位置)
本篇文章主要介紹了android仿微信通訊錄搜索示例(匹配拼音,字母,索引位置),具有一定的參考價值,有興趣的可以了解一下2017-09-09
詳解Android Studio正式簽名進行調(diào)試的實現(xiàn)步驟
這篇文章主要介紹了詳解Android Studio正式簽名進行調(diào)試的實現(xiàn)步驟的相關(guān)資料,需要的朋友可以參考下2017-07-07

