资源简介
package com.wwj.datetimepicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
/**
* 日期时间选择控件 使用方法: private EditText inputDate;//需要设置的日期时间文本编辑框 private String
* initDateTime="2012年9月3日 14:44",//初始日期时间值 在点击事件中使用:
* inputDate.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { DateTimePickDialogUtil
* dateTimePicKDialog=new
* DateTimePickDialogUtil(SinvestigateActivity.this,initDateTime);
* dateTimePicKDialog.dateTimePicKDialog(inputDate);
*
* } });
*
* @author
*/
public class DateTimePickDialogUtil implements OnDateChangedListener,
OnTimeChangedListener {
private DatePicker datePicker;
private TimePicker timePicker;
private AlertDialog ad;
private String dateTime;
private String initDateTime;
private Activity activity;
/**
* 日期时间弹出选择框构造函数
*
* @param activity
* :调用的父activity
* @param initDateTime
* 初始日期时间值,作为弹出窗口的标题和日期时间初始值
*/
public DateTimePickDialogUtil(Activity activity, String initDateTime) {
this.activity = activity;
this.initDateTime = initDateTime;
}
public void init(DatePicker datePicker, TimePicker timePicker) {
Calendar calendar = Calendar.getInstance();
if (!(null == initDateTime || "".equals(initDateTime))) {
calendar = this.getCalendarByInintData(initDateTime);
} else {
initDateTime = calendar.get(Calendar.YEAR) "年"
calendar.get(Calendar.MONTH) "月"
calendar.get(Calendar.DAY_OF_MONTH) "日 "
calendar.get(Calendar.HOUR_OF_DAY) ":"
calendar.get(Calendar.MINUTE);
}
datePicker.init(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), this);
timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
}
/**
* 弹出日期时间选择框方法
*
* @param inputDate
* :为需要设置的日期时间文本编辑框
* @return
*/
public AlertDialog dateTimePicKDialog(final EditText inputDate) {
LinearLayout dateTimeLayout = (LinearLayout) activity
.getLayoutInflater().inflate(R.layout.common_datetime, null);
datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);
timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.timepicker);
init(datePicker, timePicker);
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(this);
ad = new AlertDialog.Builder(activity)
.setTitle(initDateTime)
.setView(dateTimeLayout)
.setPositiveButton("设置", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
inputDate.setText(dateTime);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
inputDate.setText("");
}
}).show();
onDateChanged(null, 0, 0, 0);
return ad;
}
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
onDateChanged(null, 0, 0, 0);
}
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// 获得日历实例
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(),
datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
dateTime = sdf.format(calendar.getTime());
ad.setTitle(dateTime);
}
/**
* 实现将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒,并赋值给calendar
*
* @param initDateTime
* 初始日期时间值 字符串型
* @return Calendar
*/
private Calendar getCalendarByInintData(String initDateTime) {
Calendar calendar = Calendar.getInstance();
// 将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒
String date = spliteString(initDateTime, "日", "index", "front"); // 日期
String time = spliteString(initDateTime, "日", "index", "back"); // 时间
Log.i("liqiang", "liqiang----0---" date);
String yearStr = spliteString(date, "年", "index", "front"); // 年份
Log.i("liqiang", "liqiang----1---" yearStr);
String monthAndDay = spliteString(date, "年", "index", "back"); // 月日
Log.i("liqiang", "liqiang----2---" monthAndDay);
String monthStr = spliteString(monthAndDay, "月", "index", "front"); // 月
Log.i("liqiang", "liqiang----3---" monthStr);
String dayStr = spliteString(monthAndDay, "月", "index", "back"); // 日
Log.i("liqiang", "liqiang----4---" dayStr);
String hourStr = spliteString(time, ":", "index", "front"); // 时
String minuteStr = spliteString(time, ":", "index", "back"); // 分
int currentYear = Integer.valueOf(yearStr.trim()).intValue();
int currentMonth = Integer.valueOf(monthStr.trim()).intValue() - 1;
int currentDay = Integer.valueOf(dayStr.trim()).intValue();
int currentHour = Integer.valueOf(hourStr.trim()).intValue();
int currentMinute = Integer.valueOf(minuteStr.trim()).intValue();
calendar.set(currentYear, currentMonth, currentDay, currentHour,
currentMinute);
return calendar;
}
/**
* 截取子串
*
* @param srcStr
* 源串
* @param pattern
* 匹配模式
* @param indexOrLast
* @param frontOrBack
* @return
*/
public static String spliteString(String srcStr, String pattern,
String indexOrLast, String frontOrBack) {
String result = "";
int loc = -1;
if (indexOrLast.equalsIgnoreCase("index")) {
loc = srcStr.indexOf(pattern); // 取得字符串第一次出现的位置
} else {
loc = srcStr.lastIndexOf(pattern); // 最后一个匹配串的位置
}
if (frontOrBack.equalsIgnoreCase("front")) {
if (loc != -1)
result = srcStr.substring(0, loc); // 截取子串
} else {
if (loc != -1)
result = srcStr.substring(loc 1, srcStr.length()); // 截取子串
}
return result;
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
/**
* 日期时间选择控件 使用方法: private EditText inputDate;//需要设置的日期时间文本编辑框 private String
* initDateTime="2012年9月3日 14:44",//初始日期时间值 在点击事件中使用:
* inputDate.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { DateTimePickDialogUtil
* dateTimePicKDialog=new
* DateTimePickDialogUtil(SinvestigateActivity.this,initDateTime);
* dateTimePicKDialog.dateTimePicKDialog(inputDate);
*
* } });
*
* @author
*/
public class DateTimePickDialogUtil implements OnDateChangedListener,
OnTimeChangedListener {
private DatePicker datePicker;
private TimePicker timePicker;
private AlertDialog ad;
private String dateTime;
private String initDateTime;
private Activity activity;
/**
* 日期时间弹出选择框构造函数
*
* @param activity
* :调用的父activity
* @param initDateTime
* 初始日期时间值,作为弹出窗口的标题和日期时间初始值
*/
public DateTimePickDialogUtil(Activity activity, String initDateTime) {
this.activity = activity;
this.initDateTime = initDateTime;
}
public void init(DatePicker datePicker, TimePicker timePicker) {
Calendar calendar = Calendar.getInstance();
if (!(null == initDateTime || "".equals(initDateTime))) {
calendar = this.getCalendarByInintData(initDateTime);
} else {
initDateTime = calendar.get(Calendar.YEAR) "年"
calendar.get(Calendar.MONTH) "月"
calendar.get(Calendar.DAY_OF_MONTH) "日 "
calendar.get(Calendar.HOUR_OF_DAY) ":"
calendar.get(Calendar.MINUTE);
}
datePicker.init(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), this);
timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
}
/**
* 弹出日期时间选择框方法
*
* @param inputDate
* :为需要设置的日期时间文本编辑框
* @return
*/
public AlertDialog dateTimePicKDialog(final EditText inputDate) {
LinearLayout dateTimeLayout = (LinearLayout) activity
.getLayoutInflater().inflate(R.layout.common_datetime, null);
datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);
timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.timepicker);
init(datePicker, timePicker);
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(this);
ad = new AlertDialog.Builder(activity)
.setTitle(initDateTime)
.setView(dateTimeLayout)
.setPositiveButton("设置", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
inputDate.setText(dateTime);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
inputDate.setText("");
}
}).show();
onDateChanged(null, 0, 0, 0);
return ad;
}
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
onDateChanged(null, 0, 0, 0);
}
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// 获得日历实例
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(),
datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
dateTime = sdf.format(calendar.getTime());
ad.setTitle(dateTime);
}
/**
* 实现将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒,并赋值给calendar
*
* @param initDateTime
* 初始日期时间值 字符串型
* @return Calendar
*/
private Calendar getCalendarByInintData(String initDateTime) {
Calendar calendar = Calendar.getInstance();
// 将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒
String date = spliteString(initDateTime, "日", "index", "front"); // 日期
String time = spliteString(initDateTime, "日", "index", "back"); // 时间
Log.i("liqiang", "liqiang----0---" date);
String yearStr = spliteString(date, "年", "index", "front"); // 年份
Log.i("liqiang", "liqiang----1---" yearStr);
String monthAndDay = spliteString(date, "年", "index", "back"); // 月日
Log.i("liqiang", "liqiang----2---" monthAndDay);
String monthStr = spliteString(monthAndDay, "月", "index", "front"); // 月
Log.i("liqiang", "liqiang----3---" monthStr);
String dayStr = spliteString(monthAndDay, "月", "index", "back"); // 日
Log.i("liqiang", "liqiang----4---" dayStr);
String hourStr = spliteString(time, ":", "index", "front"); // 时
String minuteStr = spliteString(time, ":", "index", "back"); // 分
int currentYear = Integer.valueOf(yearStr.trim()).intValue();
int currentMonth = Integer.valueOf(monthStr.trim()).intValue() - 1;
int currentDay = Integer.valueOf(dayStr.trim()).intValue();
int currentHour = Integer.valueOf(hourStr.trim()).intValue();
int currentMinute = Integer.valueOf(minuteStr.trim()).intValue();
calendar.set(currentYear, currentMonth, currentDay, currentHour,
currentMinute);
return calendar;
}
/**
* 截取子串
*
* @param srcStr
* 源串
* @param pattern
* 匹配模式
* @param indexOrLast
* @param frontOrBack
* @return
*/
public static String spliteString(String srcStr, String pattern,
String indexOrLast, String frontOrBack) {
String result = "";
int loc = -1;
if (indexOrLast.equalsIgnoreCase("index")) {
loc = srcStr.indexOf(pattern); // 取得字符串第一次出现的位置
} else {
loc = srcStr.lastIndexOf(pattern); // 最后一个匹配串的位置
}
if (frontOrBack.equalsIgnoreCase("front")) {
if (loc != -1)
result = srcStr.substring(0, loc); // 截取子串
} else {
if (loc != -1)
result = srcStr.substring(loc 1, srcStr.length()); // 截取子串
}
return result;
}
}
代码片段和文件信息
/** Automatically generated file. DO NOT MODIFY */
package com.wwj.datetimepicker;
public final class BuildConfig {
public final static boolean DEBUG = true;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 475 2016-01-07 16:31 DateTimePicker\.classpath
文件 850 2013-03-12 18:29 DateTimePicker\.project
文件 55 2014-08-23 15:13 DateTimePicker\.settings\org.eclipse.core.resources.prefs
文件 598 2013-03-12 18:58 DateTimePicker\.settings\org.eclipse.jdt.core.prefs
文件 1107 2016-01-08 11:57 DateTimePicker\AndroidManifest.xm
文件 1107 2016-01-22 09:32 DateTimePicker\bin\AndroidManifest.xm
文件 353 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\BuildConfig.class
文件 1303 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeDemo1$1.class
文件 1258 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeDemo1$2.class
文件 1099 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeDemo1$on
文件 4023 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeDemo1.class
文件 1259 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickDialogUtil$1.class
文件 1169 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickDialogUtil$2.class
文件 6772 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickDialogUtil.class
文件 1338 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickerActivity$1.class
文件 1338 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickerActivity$2.class
文件 1770 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickerActivity.class
文件 1017 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$1.class
文件 889 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$2.class
文件 924 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$3$1.class
文件 1279 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$3.class
文件 900 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$4$1.class
文件 1271 2016-01-22 09:32 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$4.class
文件 2246 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe.class
文件 352 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\R$attr.class
文件 419 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\R$drawable.class
文件 741 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\R$id.class
文件 511 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\R$layout.class
文件 482 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\R$string.class
文件 559 2016-01-26 10:48 DateTimePicker\bin\classes\com\wwj\datetimepicker\R.class
............此处省略58个文件信息
- 上一篇:文件管理器
- 下一篇:Android实现聊天app
相关资源
- android 数字以动画的形式动态增长 源
- 西玛拉雅FM调用
- android 仿微信主界面UI(不附代码逻辑
- android 点击两次返回键退出
- android崩溃日志保存SDCARD-本地阅读-并
- 雅圆字体 otf艺术字体
- android 拍照摄像 回放代码
- android 自定义进度加载条Demo
- android 即时通讯 IM(MobileIMSDK)
- android 详情页导航栏颜色渐变
- android 拖动浏览大图片(类似地图拖动
- Android_AlertDialog代码
- android 评分自定义控件 以及
- android 白天 夜间模式切换
- android grid 分页(美团九宫格导航)
-
android 解析xm
l - android分类标题框架demo(SmartTabLayout)
- android 端视频拍摄(带进度条)
- Android 2秒内按两次back键退出应用
- 一个基于RSS的新闻应用
- android 息屏亮屏 (呼吸效果)
- 精仿微信程序android源码
- 《kotlin-for-android-developers-zh》(kotli
- android bluetooth 搜尋距離
- Android应用源码追加的6中图片特效处理
- 完美解决android_gif_drawable_demo
- android 调整屏幕亮度 (无需root)
- android通过ajax上传文件到php服务器
- android 联动滚轮(选择控件)
-
android_自定义ti
tleBar
评论
共有 条评论