博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
完全参照系统自带的DatePickerDialog、TimePickerDialog的源代码仿写的DateTimePickerDialog...
阅读量:5924 次
发布时间:2019-06-19

本文共 6739 字,大约阅读时间需要 22 分钟。

完全参照系统自带的DatePickerDialog、TimePickerDialog的源代码仿写的DateTimePickerDialog。具有同时选择日期、时间的功能。

在2.2、2.3平台,显示的效果可能会有一个大背景框在后面。4.0以上平台无此现象。此怪异效果通过各种手段未能解决。功能不影响正常使用。

import java.util.Calendar;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.os.Bundle;import android.text.format.DateUtils;import android.view.Gravity;import android.view.ViewGroup.LayoutParams;import android.widget.DatePicker;import android.widget.DatePicker.OnDateChangedListener;import android.widget.LinearLayout;import android.widget.TimePicker;import android.widget.TimePicker.OnTimeChangedListener;/** * 完全参照系统自带的DatePickerDialog、TimePickerDialog的源代码仿写的DateTimePickerDialog。具有同时选择日期、时间的功能。
* 在2.2、2.3平台,显示的效果可能会有一个大背景框在后面。4.0以上平台无此现象。怪异的原因未能解决。 * @author AsionTang * @since 2014年2月26日 17:02:24 */public class DateTimePickerDialog extends AlertDialog implements OnClickListener, OnDateChangedListener, OnTimeChangedListener{ private final OnDateTimeChangedListener mCallback; private final DatePicker mDatePicker; private final TimePicker mTimePicker; private static final String HOUR = "hour"; private static final String MINUTE = "minute"; private static final String IS_24_HOUR = "is24hour"; private static final String YEAR = "year"; private static final String MONTH = "month"; private static final String DAY = "day"; private final Calendar mCalendar; public DateTimePickerDialog(final Context context, final int theme, final OnDateTimeChangedListener callBack) { super(context, theme); this.mCallback = callBack; this.mCalendar = Calendar.getInstance(); this.updateTitle(); this.setButton(BUTTON_POSITIVE, context.getText(android.R.string.ok), this); this.setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel), (OnClickListener) null); this.mDatePicker = new DatePicker(context); this.mDatePicker.init(this.mCalendar.get(Calendar.YEAR), this.mCalendar.get(Calendar.MONTH), this.mCalendar.get(Calendar.DAY_OF_MONTH), this); this.mTimePicker = new TimePicker(context); this.mTimePicker.setIs24HourView(true); this.mTimePicker.setCurrentHour(this.mCalendar.get(Calendar.HOUR_OF_DAY)); this.mTimePicker.setCurrentMinute(this.mCalendar.get(Calendar.MINUTE)); this.mTimePicker.setOnTimeChangedListener(this); final LinearLayout linearLayout = new LinearLayout(context); linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); linearLayout.setOrientation(LinearLayout.VERTICAL);//默认为HORIZONTAL(水平) linearLayout.setGravity(Gravity.CENTER); linearLayout.addView(this.mDatePicker); linearLayout.addView(this.mTimePicker); this.setView(linearLayout); } public DateTimePickerDialog(final Context context, final OnDateTimeChangedListener callBack) { this(context, 0, callBack); } /** * Gets the {
@link DatePicker} contained in this dialog. * * @return The DatePicker view. */ public DatePicker getDatePicker() { return this.mDatePicker; } /** * Gets the {
@link TimePicker} contained in this dialog. * * @return The TimePicker view. */ public TimePicker getTimePicker() { return this.mTimePicker; } @Override public void onClick(final DialogInterface dialog, final int which) { this.tryNotifyDateTimeSet(); } @Override public void onDateChanged(final DatePicker view, final int year, final int month, final int day) { this.mDatePicker.init(year, month, day, this); this.mCalendar.set(Calendar.YEAR, year); this.mCalendar.set(Calendar.MONTH, month); this.mCalendar.set(Calendar.DAY_OF_MONTH, day); this.updateTitle(); } @Override public void onRestoreInstanceState(final Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); final int year = savedInstanceState.getInt(YEAR); final int month = savedInstanceState.getInt(MONTH); final int day = savedInstanceState.getInt(DAY); this.mDatePicker.init(year, month, day, this); final int hour = savedInstanceState.getInt(HOUR); final int minute = savedInstanceState.getInt(MINUTE); this.mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR)); this.mTimePicker.setCurrentHour(hour); this.mTimePicker.setCurrentMinute(minute); } @Override public Bundle onSaveInstanceState() { final Bundle state = super.onSaveInstanceState(); state.putInt(YEAR, this.mDatePicker.getYear()); state.putInt(MONTH, this.mDatePicker.getMonth()); state.putInt(DAY, this.mDatePicker.getDayOfMonth()); state.putInt(HOUR, this.mTimePicker.getCurrentHour()); state.putInt(MINUTE, this.mTimePicker.getCurrentMinute()); state.putBoolean(IS_24_HOUR, this.mTimePicker.is24HourView()); return state; } @Override public void onTimeChanged(final TimePicker view, final int hourOfDay, final int minute) { this.mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay); this.mCalendar.set(Calendar.MINUTE, minute); } public void setDate(final int year, final int monthOfYear, final int dayOfMonth) { this.mDatePicker.updateDate(year, monthOfYear, dayOfMonth); } public void setDateAndTime(final int year, final int monthOfYear, final int dayOfMonth, final int hour, final int minute, final boolean is24HourView) { this.setDate(year, monthOfYear, dayOfMonth); this.setTime(hour, minute, is24HourView); } public void setTime(final int hour, final int minute, final boolean is24HourView) { this.mTimePicker.setIs24HourView(is24HourView); this.mTimePicker.setCurrentHour(hour); this.mTimePicker.setCurrentMinute(minute); } private void tryNotifyDateTimeSet() { if (this.mCallback != null) { this.mDatePicker.clearFocus(); this.mTimePicker.clearFocus(); this.mCallback.onDateTimeChanged(this.mDatePicker, this.mTimePicker// , this.mDatePicker.getYear(), this.mDatePicker.getMonth(), this.mDatePicker.getDayOfMonth()// , this.mTimePicker.getCurrentHour(), this.mTimePicker.getCurrentMinute()); } } private void updateTitle() { final String title = DateUtils.formatDateTime(this.getContext(), this.mCalendar.getTimeInMillis()// , DateUtils.FORMAT_SHOW_DATE// | DateUtils.FORMAT_NO_YEAR// | DateUtils.FORMAT_SHOW_WEEKDAY); this.setTitle(title); } public interface OnDateTimeChangedListener { void onDateTimeChanged(DatePicker dateView, TimePicker timeView, int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minute); }}

 

转载于:https://www.cnblogs.com/AsionTang/p/3569695.html

你可能感兴趣的文章
唠唠 RDS 那些事 ——发布虚拟机
查看>>
极好的数据库学习资料
查看>>
tomcat service启动脚本
查看>>
linux入门之文本处理工具sed粗略使用(1)
查看>>
C语言生成随机数保存在文件中
查看>>
java语言匿名内部类之回调
查看>>
ansible调用callbacks插件实现结果nosql输出回调
查看>>
利用python多线程和队列管理shell程序
查看>>
Greenplum迁移数据后批量更新序列值
查看>>
Spring学习笔记六--外部配置文件
查看>>
代理模式
查看>>
zabbix监控linux tcp连接数
查看>>
【AD】AD通用工具
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
intellij idea搭建Java web环境
查看>>
NFS on AIX
查看>>
CIO十大利器:如何延长数据中心的使用寿命
查看>>
Makefile中制定动态库路径的问题
查看>>
volatile关键字
查看>>