android代码记录日期,Android日期和时间选择器实现代码
/**
* 選擇日期和時間
*/
private void selectDataAndTime() {
// 獲取當前時間
final Calendar calendar = Calendar.getInstance();
/*
* toast("當前時間是:" + 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);
}
總結
以上是生活随笔為你收集整理的android代码记录日期,Android日期和时间选择器实现代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言 文件截断,c – 是否有一种正统
- 下一篇: sketch生成android代码,ja