实现的效果就是在同一个布局上显示日期选择和时间选择,时间不准确bug修复
1、自定义类DateTimePickDialogUtil.java
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.setIs24HourView(true); 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"); // 时间 String yearStr = spliteString(date, "年", "index", "front"); // 年份 String monthAndDay = spliteString(date, "年", "index", "back"); // 月日 String monthStr = spliteString(monthAndDay, "月", "index", "front"); // 月 String dayStr = spliteString(monthAndDay, "月", "index", "back"); // 日 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; } }
2、自定义界面common_datetime.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <DatePicker 8 android:id="@+id/datepicker" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:calendarViewShown="false" /> 12 13 <TimePicker 14 android:id="@+id/timepicker" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" /> 17 18 </LinearLayout>
3、DateTimePickerActivity.java
1 public class DateTimePickerActivity extends Activity { 2 /** Called when the activity is first created. */ 3 private EditText startDateTime; 4 private EditText endDateTime; 5 6 //private String initStartDateTime = "2013年9月3日 14:44"; // 初始化开始时间 7 private String initStartDateTime = null; // 初始化开始时间 8 private String initEndDateTime = null; // 初始化结束时间 9 10 @Override 11 public void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.main); 14 15 // 两个输入框 16 startDateTime = (EditText) findViewById(R.id.inputDate); 17 endDateTime = (EditText) findViewById(R.id.inputDate2); 18 19 startDateTime.setText(initStartDateTime); 20 endDateTime.setText(initEndDateTime); 21 22 startDateTime.setOnClickListener(new OnClickListener() { 23 public void onClick(View v) { 24 25 DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil( 26 DateTimePickerActivity.this, startDateTime.getText()==null?null:startDateTime.getText().toString()); 27 dateTimePicKDialog.dateTimePicKDialog(startDateTime); 28 29 } 30 }); 31 32 endDateTime.setOnClickListener(new OnClickListener() { 33 34 public void onClick(View v) { 35 DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil( 36 DateTimePickerActivity.this, endDateTime.getText()==null?null:endDateTime.getText().toString()); 37 dateTimePicKDialog.dateTimePicKDialog(endDateTime); 38 } 39 }); 40 } 41 }