参考别人的:http://blog.csdn.net/wwj_748/article/details/38778631
package com.example.demo; import java.text.SimpleDateFormat; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; 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; public class DateTime implements OnDateChangedListener, OnTimeChangedListener { private DatePicker datePicker; private TimePicker timePicker; private AlertDialog ad; private String dateTime; private String initDateTime; private Activity activity; public DateTime(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)); } 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); } private Calendar getCalendarByInintData(String initDateTime) { Calendar calendar = Calendar.getInstance(); 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; } 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; } }
extends Activity { /** Called when the activity is first created. */ private EditText startDateTime; private EditText endDateTime; private String initStartDateTime = "2013年9月3日 14:44"; // 初始化开始时间 private String initEndDateTime = "2014年8月23日 17:44"; // 初始化结束时间 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startDateTime = (EditText) findViewById(R.id.inputDate); endDateTime = (EditText) findViewById(R.id.inputDate2); startDateTime.setText(initStartDateTime); endDateTime.setText(initEndDateTime); startDateTime.setOnClickListener(new OnClickListener() { public void onClick(View v) { DateTime dateTimePicKDialog = new DateTime( DateTimePicker.this, initEndDateTime); dateTimePicKDialog.dateTimePicKDialog(startDateTime); } }); endDateTime.setOnClickListener(new OnClickListener() { public void onClick(View v) { DateTime dateTimePicKDialog = new DateTime( DateTimePicker.this, initEndDateTime); dateTimePicKDialog.dateTimePicKDialog(endDateTime); } }); } }
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <DatePicker android:id="@+id/datepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:calendarViewShown="false" /> <TimePicker android:id="@+id/timepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:background="@drawable/a" > <!-- <ImageView android:id="@+id/dynamic_item_image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:layout_marginTop="0dip" android:adjustViewBounds="true" android:background="@drawable/a" /> --> <TextView android:layout_width="fill_parent" android:layout_height="40dip" android:text="开始时间" android:textSize="28sp" android:textColor="#CD0000" android:gravity="center" /> <EditText android:id="@+id/inputDate" android:layout_width="fill_parent" android:layout_height="80dip" android:focusable="false" android:inputType="none" android:gravity="center" android:background="#4876FF" /> <TextView android:layout_width="fill_parent" android:layout_height="40dip" android:text="结束时间" android:textSize="28sp" android:textColor="#CD0000" android:gravity="center" /> <EditText android:id="@+id/inputDate2" android:layout_width="fill_parent" android:layout_height="80dip" android:focusable="false" android:inputType="none" android:gravity="center" android:background="#4876FF" /> </LinearLayout>