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;
|
002 |
003 |
import java.text.SimpleDateFormat;
|
004 |
import java.util.Calendar;
|
005 |
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;
|
018 |
/** |
019 |
* 日期时间选择控件
|
020 |
* @author 大漠
|
021 |
*/
|
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;
|
029 |
|
030 |
/**
|
031 |
* 日期时间弹出选择框构
|
032 |
* @param activity:调用的父activity
|
033 |
*/
|
034 |
public DateTimePickerDialog(Activity activity)
|
035 |
{
|
036 |
this .activity = activity;
|
037 |
}
|
038 |
|
039 |
public void init(DatePicker datePicker,TimePicker timePicker)
|
040 |
{
|
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));
|
050 |
}
|
051 |
|
052 |
/**
|
053 |
* 弹出日期时间选择框
|
054 |
* @param dateTimeTextEdite 需要设置的日期时间文本编辑框
|
055 |
* @param type: 0为日期时间类型:yyyy-MM-dd HH:mm:ss
|
056 |
* 1为日期类型:yyyy-MM-dd
|
057 |
* 2为时间类型:HH:mm:ss
|
058 |
* @return
|
059 |
*/
|
060 |
public AlertDialog dateTimePicKDialog( final EditText dateTimeTextEdite, int type)
|
061 |
{
|
062 |
Calendar c = Calendar.getInstance();
|
063 |
switch (type) {
|
064 |
case 1 :
|
065 |
new DatePickerDialog(activity,
|
066 |
new DatePickerDialog.OnDateSetListener() {
|
067 |
public void onDateSet(DatePicker datePicker, int year, int monthOfYear,
|
068 |
int dayOfMonth) {
|
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);
|
075 |
}
|
076 |
},
|
077 |
c.get(Calendar.YEAR),
|
078 |
c.get(Calendar.MONTH),
|
079 |
c.get(Calendar.DATE)).show();
|
080 |
return null ;
|
081 |
case 2 :
|
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);
|
092 |
}
|
093 |
},
|
094 |
c.get(Calendar.HOUR_OF_DAY),
|
095 |
c.get(Calendar.MINUTE),
|
096 |
true ).show();
|
097 |
return null ;
|
098 |
default :
|
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 );
|
105 |
|
106 |
ad = new AlertDialog.Builder(activity).setIcon(R.drawable.datetimeicon).setTitle(initDateTime).setView(dateTimeLayout).setPositiveButton( "设置" ,
|
107 |
new DialogInterface.OnClickListener()
|
108 |
{
|
109 |
public void onClick(DialogInterface dialog,
|
110 |
int whichButton)
|
111 |
{
|
112 |
dateTimeTextEdite.setText(dateTime);
|
113 |
}
|
114 |
}).setNegativeButton( "取消" ,
|
115 |
new DialogInterface.OnClickListener()
|
116 |
{
|
117 |
public void onClick(DialogInterface dialog,
|
118 |
int whichButton)
|
119 |
{
|
120 |
dateTimeTextEdite.setText( "" );
|
121 |
}
|
122 |
}).show();
|
123 |
|
124 |
onDateChanged( null , 0 , 0 , 0 );
|
125 |
return ad;
|
126 |
}
|
127 |
}
|
128 |
|
129 |
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
|
130 |
{
|
131 |
onDateChanged( null , 0 , 0 , 0 );
|
132 |
}
|
133 |
134 |
public void onDateChanged(DatePicker view, int year, int monthOfYear,
|
135 |
int dayOfMonth)
|
136 |
{
|
137 |
Calendar calendar = Calendar.getInstance();
|
138 |
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);
|
145 |
}
|
146 |
|
147 |
} |
布局文件:
datetime.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?>
|
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
|
03 |
android:orientation = "vertical"
|
04 |
android:layout_width = "fill_parent"
|
05 |
android:layout_height = "fill_parent" >
|
06 |
< DatePicker
|
07 |
android:id = "@+id/datepicker"
|
08 |
android:layout_width = "fill_parent"
|
09 |
android:layout_height = "wrap_content" />
|
10 |
< TimePicker
|
11 |
android:id = "@+id/timepicker"
|
12 |
android:layout_width = "fill_parent"
|
13 |
android:layout_height = "wrap_content" />
|
14 |
</ LinearLayout >
|
使用实例:
MainActivity.java
01 |
package me.gogogogo.activity;
|
02 |
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;
|
09 |
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);
|
17 |
|
18 |
datetime = (EditText) this .findViewById(R.id.datetime);
|
19 |
date = (EditText) this .findViewById(R.id.date);
|
20 |
time = (EditText) this .findViewById(R.id.time);
|
21 |
|
22 |
datetime.setOnClickListener( new DateTimeOnClick());
|
23 |
date.setOnClickListener( new DateOnClick());
|
24 |
time.setOnClickListener( new TimeOnClick());
|
25 |
}
|
26 |
|
27 |
private final class DateTimeOnClick implements OnClickListener{
|
28 |
public void onClick(View v) {
|
29 |
DateTimePickerDialog dateTimePicKDialog = new DateTimePickerDialog(
|
30 |
MainActivity. this );
|
31 |
dateTimePicKDialog.dateTimePicKDialog(datetime, 0 );
|
32 |
}
|
33 |
}
|
34 |
|
35 |
private final class DateOnClick implements OnClickListener{
|
36 |
public void onClick(View v) {
|
37 |
DateTimePickerDialog dateTimePicKDialog = new DateTimePickerDialog(
|
38 |
MainActivity. this );
|
39 |
dateTimePicKDialog.dateTimePicKDialog(date, 1 );
|
40 |
}
|
41 |
}
|
42 |
|
43 |
private final class TimeOnClick implements OnClickListener{
|
44 |
public void onClick(View v) {
|
45 |
DateTimePickerDialog dateTimePicKDialog = new DateTimePickerDialog(
|
46 |
MainActivity. this );
|
47 |
dateTimePicKDialog.dateTimePicKDialog(time, 2 );
|
48 |
}
|
49 |
}
|
50 |
} |
main.xml
01 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
|
02 |
xmlns:tools = "http://schemas.android.com/tools"
|
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" />
|
13 |
|
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" />
|
21 |
|
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" />
|
29 |
30 |
</ LinearLayout >
|
实例效果: