Android中实现日期时间选择器(DatePicker和TimePicker) - %幻#影%

时间:2024-03-07 07:10:12

Android中实现日期时间选择器(DatePicker和TimePicker)

利用Android应用框架提供的DatePicker(日期选择器)和TimePicker(时间选择器),实现日期时间选择器。

Dialog的Content布局文件(date_time_dialog.xml):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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" 
    android:padding="10dip"
   
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:singleLine="true" 
        android:text="请选择日期" 
        android:textColor="#FFFFFF" 
        android:textSize="16sp" /> 
   
    <DatePicker 
        android:id="@+id/date_picker" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="5dip" /> 
   
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dip" 
        android:singleLine="true" 
        android:text="请选择时间" 
        android:textColor="#FFFFFF" 
        android:textSize="16sp" /> 
   
    <TimePicker 
        android:id="@+id/time_picker" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="5dip" /> 
   
</LinearLayout>

 

代码中的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.easipass.test; 
   
import java.util.Calendar; 
   
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.text.InputType; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.TimePicker; 
   
/**
 * 功能描述:实现日期时间选择器
 
 * @author android_ls
 */ 
public class DateTimeActivity extends Activity implements View.OnTouchListener { 
      
    private EditText etStartTime; 
   
    private EditText etEndTime; 
       
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
           
        etStartTime = (EditText) this.findViewById(R.id.et_start_time); 
        etEndTime = (EditText) this.findViewById(R.id.et_end_time); 
           
        etStartTime.setOnTouchListener(this); 
        etEndTime.setOnTouchListener(this); 
           
    
   
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
   
            AlertDialog.Builder builder = new AlertDialog.Builder(this); 
            View view = View.inflate(this, R.layout.date_time_dialog, null); 
            final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker); 
            final TimePicker timePicker = (android.widget.TimePicker) view.findViewById(R.id.time_picker); 
            builder.setView(view); 
   
            Calendar cal = Calendar.getInstance(); 
            cal.setTimeInMillis(System.currentTimeMillis()); 
            datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null); 
   
            timePicker.setIs24HourView(true); 
            timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY)); 
            timePicker.setCurrentMinute(Calendar.MINUTE); 
   
            if (v.getId() == R.id.et_start_time) { 
                final int inType = etStartTime.getInputType(); 
                etStartTime.setInputType(InputType.TYPE_NULL); 
                etStartTime.onTouchEvent(event); 
                etStartTime.setInputType(inType); 
                etStartTime.setSelection(etStartTime.getText().length()); 
                   
                builder.setTitle("选取起始时间"); 
                builder.setPositiveButton("确  定"new DialogInterface.OnClickListener() { 
   
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
   
                        StringBuffer sb = new StringBuffer(); 
                        sb.append(String.format("%d-%02d-%02d",  
                                datePicker.getYear(),  
                                datePicker.getMonth() + 1
                                datePicker.getDayOfMonth())); 
                        sb.append("  "); 
                        sb.append(timePicker.getCurrentHour()) 
                        .append(":").append(timePicker.getCurrentMinute()); 
   
                        etStartTime.setText(sb); 
                        etEndTime.requestFocus(); 
                           
                        dialog.cancel(); 
                    
                }); 
                   
            else if (v.getId() == R.id.et_end_time) { 
                int inType = etEndTime.getInputType(); 
                etEndTime.setInputType(InputType.TYPE_NULL);     
                etEndTime.onTouchEvent(event); 
                etEndTime.setInputType(inType); 
                etEndTime.setSelection(etEndTime.getText().length()); 
   
                builder.setTitle("选取结束时间"); 
                builder.setPositiveButton("确  定"new DialogInterface.OnClickListener() { 
   
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
   
                        StringBuffer sb = new StringBuffer(); 
                        sb.append(String.format("%d-%02d-%02d",  
                                datePicker.getYear(),  
                                datePicker.getMonth() + 1,  
                                datePicker.getDayOfMonth())); 
                        sb.append("  "); 
                        sb.append(timePicker.getCurrentHour()) 
                        .append(":").append(timePicker.getCurrentMinute()); 
                        etEndTime.setText(sb); 
                           
                        dialog.cancel(); 
                    
                }); 
            
               
            Dialog dialog = builder.create(); 
            dialog.show(); 
        
   
        return true
    
       
}

 

运行后的效果图:

 Android中实现日期时间选择器(DatePicker和TimePicker)

点击EditTet之后

Android中实现日期时间选择器(DatePicker和TimePicker)

点击确定之后

Android中实现日期时间选择器(DatePicker和TimePicker)