Android系统提供了DatePicker、TimePicker、DatePickerDialog、TimePickerDialog控件用于日期和时间的选择和设置,但是它们都是相对独立的,不能方便的设置“yyyy-MM-dd HH:mm:ss”型日期时间,下面是使用这几种组件自定义的一个日期时间选择控件DateTimePickerDialog,可以方便的设置“yyyy-MM-dd HH:mm:ss”型日期时间,“yyyy-MM-dd”型日期和“HH:mm:ss”型时间,如下图:
代码实现:
DateTimePickerDialog.java
001 | package me.gogogogo.util; |
003 | import java.text.SimpleDateFormat; |
004 | import java.util.Calendar; |
006 | import me.gogogogo.activity.R; |
007 | import android.app.Activity; |
008 | import android.app.AlertDialog; |
009 | import android.app.DatePickerDialog; |
010 | import android.app.TimePickerDialog; |
011 | import android.content.DialogInterface; |
012 | import android.widget.DatePicker; |
013 | import android.widget.EditText; |
014 | import android.widget.LinearLayout; |
015 | import android.widget.DatePicker.OnDateChangedListener; |
016 | import android.widget.TimePicker; |
017 | import android.widget.TimePicker.OnTimeChangedListener; |
022 | public class DateTimePickerDialog implements OnDateChangedListener,OnTimeChangedListener{ |
023 | private DatePicker datePicker; |
024 | private TimePicker timePicker; |
025 | private AlertDialog ad; |
026 | private String dateTime; |
027 | private String initDateTime; |
028 | private Activity activity; |
032 | * @param activity:调用的父activity |
034 | public DateTimePickerDialog(Activity activity) |
036 | this .activity = activity; |
039 | public void init(DatePicker datePicker,TimePicker timePicker) |
041 | Calendar calendar= Calendar.getInstance(); |
042 | initDateTime=calendar.get(Calendar.YEAR)+ "-" +calendar.get(Calendar.MONTH)+ "-" + |
043 | calendar.get(Calendar.DAY_OF_MONTH)+ " " + |
044 | calendar.get(Calendar.HOUR_OF_DAY)+ ":" + |
045 | calendar.get(Calendar.MINUTE)+ |
046 | calendar.get(Calendar.SECOND); |
047 | datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH), this ); |
048 | timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY)); |
049 | timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE)); |
054 | * @param dateTimeTextEdite 需要设置的日期时间文本编辑框 |
055 | * @param type: 0为日期时间类型:yyyy-MM-dd HH:mm:ss |
060 | public AlertDialog dateTimePicKDialog( final EditText dateTimeTextEdite, int type) |
062 | Calendar c = Calendar.getInstance(); |
065 | new DatePickerDialog(activity, |
066 | new DatePickerDialog.OnDateSetListener() { |
067 | public void onDateSet(DatePicker datePicker, int year, int monthOfYear, |
069 | Calendar calendar = Calendar.getInstance(); |
070 | calendar.set(datePicker.getYear(), datePicker.getMonth(), |
071 | datePicker.getDayOfMonth()); |
072 | SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); |
073 | dateTime=sdf.format(calendar.getTime()); |
074 | dateTimeTextEdite.setText(dateTime); |
077 | c.get(Calendar.YEAR), |
078 | c.get(Calendar.MONTH), |
079 | c.get(Calendar.DATE)).show(); |
082 | new TimePickerDialog(activity, |
083 | new TimePickerDialog.OnTimeSetListener() { |
084 | public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) { |
085 | Calendar calendar = Calendar.getInstance(); |
086 | calendar.set(Calendar.YEAR, Calendar.MONTH, |
087 | Calendar.DAY_OF_MONTH, timePicker.getCurrentHour(), |
088 | timePicker.getCurrentMinute()); |
089 | SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm:ss" ); |
090 | dateTime=sdf.format(calendar.getTime()); |
091 | dateTimeTextEdite.setText(dateTime); |
094 | c.get(Calendar.HOUR_OF_DAY), |
095 | c.get(Calendar.MINUTE), |
099 | LinearLayout dateTimeLayout = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.datetime, null ); |
100 | datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker); |
101 | timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.timepicker); |
102 | init(datePicker,timePicker); |
103 | timePicker.setIs24HourView( true ); |
104 | timePicker.setOnTimeChangedListener( this ); |
106 | ad = new AlertDialog.Builder(activity).setIcon(R.drawable.datetimeicon).setTitle(initDateTime).setView(dateTimeLayout).setPositiveButton( "设置" , |
107 | new DialogInterface.OnClickListener() |
109 | public void onClick(DialogInterface dialog, |
112 | dateTimeTextEdite.setText(dateTime); |
114 | }).setNegativeButton( "取消" , |
115 | new DialogInterface.OnClickListener() |
117 | public void onClick(DialogInterface dialog, |
120 | dateTimeTextEdite.setText( "" ); |
124 | onDateChanged( null , 0 , 0 , 0 ); |
129 | public void onTimeChanged(TimePicker view, int hourOfDay, int minute) |
131 | onDateChanged( null , 0 , 0 , 0 ); |
134 | public void onDateChanged(DatePicker view, int year, int monthOfYear, |
137 | Calendar calendar = Calendar.getInstance(); |
139 | calendar.set(datePicker.getYear(), datePicker.getMonth(), |
140 | datePicker.getDayOfMonth(), timePicker.getCurrentHour(), |
141 | timePicker.getCurrentMinute()); |
142 | SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); |
143 | dateTime=sdf.format(calendar.getTime()); |
144 | ad.setTitle(dateTime); |
布局文件:
datetime.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" > |
07 | android:id = "@+id/datepicker" |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "wrap_content" /> |
11 | android:id = "@+id/timepicker" |
12 | android:layout_width = "fill_parent" |
13 | android:layout_height = "wrap_content" /> |
使用实例:
MainActivity.java
01 | package me.gogogogo.activity; |
03 | import me.gogogogo.util.DateTimePickerDialog; |
04 | import android.os.Bundle; |
05 | import android.view.View; |
06 | import android.view.View.OnClickListener; |
07 | import android.widget.EditText; |
08 | import android.app.Activity; |
10 | public class MainActivity extends Activity { |
11 | private EditText date; |
12 | private EditText time; |
13 | private EditText datetime; |
14 | public void onCreate(Bundle savedInstanceState) { |
15 | super .onCreate(savedInstanceState); |
16 | setContentView(R.layout.main); |
18 | datetime = (EditText) this .findViewById(R.id.datetime); |
19 | date = (EditText) this .findViewById(R.id.date); |
20 | time = (EditText) this .findViewById(R.id.time); |
22 | datetime.setOnClickListener( new DateTimeOnClick()); |
23 | date.setOnClickListener( new DateOnClick()); |
24 | time.setOnClickListener( new TimeOnClick()); |
27 | private final class DateTimeOnClick implements OnClickListener{ |
28 | public void onClick(View v) { |
29 | DateTimePickerDialog dateTimePicKDialog = new DateTimePickerDialog( |
31 | dateTimePicKDialog.dateTimePicKDialog(datetime, 0 ); |
35 | private final class DateOnClick implements OnClickListener{ |
36 | public void onClick(View v) { |
37 | DateTimePickerDialog dateTimePicKDialog = new DateTimePickerDialog( |
39 | dateTimePicKDialog.dateTimePicKDialog(date, 1 ); |
43 | private final class TimeOnClick implements OnClickListener{ |
44 | public void onClick(View v) { |
45 | DateTimePickerDialog dateTimePicKDialog = new DateTimePickerDialog( |
47 | dateTimePicKDialog.dateTimePicKDialog(time, 2 ); |
main.xml
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" > |
06 | < TextView android:text = "@string/datetime" |
07 | android:layout_width = "wrap_content" |
08 | android:layout_height = "wrap_content" /> |
09 | < EditText android:id = "@+id/datetime" |
10 | android:layout_width = "fill_parent" |
11 | android:layout_height = "wrap_content" |
12 | android:inputType = "datetime" /> |
14 | < TextView android:text = "@string/date" |
15 | android:layout_width = "wrap_content" |
16 | android:layout_height = "wrap_content" /> |
17 | < EditText android:id = "@+id/date" |
18 | android:layout_width = "fill_parent" |
19 | android:layout_height = "wrap_content" |
20 | android:inputType = "date" /> |
22 | < TextView android:text = "@string/time" |
23 | android:layout_width = "wrap_content" |
24 | android:layout_height = "wrap_content" /> |
25 | < EditText android:id = "@+id/time" |
26 | android:layout_width = "fill_parent" |
27 | android:layout_height = "wrap_content" |
28 | android:inputType = "time" /> |
实例效果: