Andorid時間控件和日期控件的Demo(代碼)

默認顯示當前時間:

                      

time.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
38
39
40
41
42
43
44
45
46
47
48
49
<strong><?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="日期和時間控件的使用DEMO"/>
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
 
        <EditText
            android:id="@+id/showdate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
 
        <Button
            android:id="@+id/pickdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="選擇日期"/>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
 
        <EditText
            android:id="@+id/showtime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
 
        <Button
            android:id="@+id/picktime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="選擇時間"/>
    </LinearLayout>
 
</LinearLayout></strong>
TimeActivity.java
?
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
publicclass TimeActivity extendsActivity {
     
    privateEditText showDate = null;
    privateButton pickDate = null;
    privateEditText showTime = null;
    privateButton pickTime = null;
     
    privatestatic final int SHOW_DATAPICK = 0;
    privatestatic final int DATE_DIALOG_ID = 1
    privatestatic final int SHOW_TIMEPICK = 2;
    privatestatic final int TIME_DIALOG_ID = 3;
     
    privateint mYear; 
    privateint mMonth;
    privateint mDay;
    privateint mHour;
    privateint mMinute;
     
    /** Called when the activity is first created. */
    @Override
    publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time);
         
        initializeViews();
         
        finalCalendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR); 
        mMonth = c.get(Calendar.MONTH); 
        mDay = c.get(Calendar.DAY_OF_MONTH);
         
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
         
        setDateTime();
        setTimeOfDay();
    }
     
    /**
     * 初始化控件和UI視圖
     */
    privatevoid initializeViews(){
        showDate = (EditText) findViewById(R.id.showdate); 
        pickDate = (Button) findViewById(R.id.pickdate);
        showTime = (EditText)findViewById(R.id.showtime);
        pickTime = (Button)findViewById(R.id.picktime);
         
        pickDate.setOnClickListener(newView.OnClickListener() {
             
            @Override
            publicvoid onClick(View v) {
               Message msg = newMessage();
               if(pickDate.equals((Button) v)) { 
                  msg.what = TimeActivity.SHOW_DATAPICK; 
               
               TimeActivity.this.dateandtimeHandler.sendMessage(msg);
            }
        });
         
        pickTime.setOnClickListener(newView.OnClickListener() {
             
            @Override
            publicvoid onClick(View v) {
               Message msg = newMessage();
               if(pickTime.equals((Button) v)) { 
                  msg.what = TimeActivity.SHOW_TIMEPICK; 
               
               TimeActivity.this.dateandtimeHandler.sendMessage(msg);
            }
        });
    }
 
    /**
     * 設置日期
     */
    privatevoid setDateTime(){
       finalCalendar c = Calendar.getInstance(); 
        
       mYear = c.get(Calendar.YEAR); 
       mMonth = c.get(Calendar.MONTH); 
       mDay = c.get(Calendar.DAY_OF_MONTH);
   
       updateDateDisplay();
    }
     
    /**
     * 更新日期顯示
     */
    privatevoid updateDateDisplay(){
       showDate.setText(newStringBuilder().append(mYear).append("-")
               .append((mMonth + 1) < 10"0"+ (mMonth + 1) : (mMonth + 1)).append("-")
               .append((mDay < 10) ? "0"+ mDay : mDay));
    }
     
    /**
     * 日期控件的事件
     */ 
    privateDatePickerDialog.OnDateSetListener mDateSetListener = newDatePickerDialog.OnDateSetListener() { 
   
       publicvoid onDateSet(DatePicker view, intyear, intmonthOfYear, 
              intdayOfMonth) { 
           mYear = year; 
           mMonth = monthOfYear; 
           mDay = dayOfMonth; 
 
           updateDateDisplay();
       
    };
     
    /**
     * 設置時間
     */
    privatevoid setTimeOfDay(){
       finalCalendar c = Calendar.getInstance();
       mHour = c.get(Calendar.HOUR_OF_DAY);
       mMinute = c.get(Calendar.MINUTE);
       updateTimeDisplay();
    }
     
    /**
     * 更新時間顯示
     */
    privatevoid updateTimeDisplay(){
       showTime.setText(newStringBuilder().append(mHour).append(":")
               .append((mMinute < 10) ? "0"+ mMinute : mMinute));
    }
     
    /**
     * 時間控件事件
     */
    privateTimePickerDialog.OnTimeSetListener mTimeSetListener = newTimePickerDialog.OnTimeSetListener() {
         
        @Override
        publicvoid onTimeSet(TimePicker view, inthourOfDay, intminute) {
            mHour = hourOfDay;
            mMinute = minute;
             
            updateTimeDisplay();
        }
    };
     
    @Override 
    protectedDialog onCreateDialog(intid) { 
       switch(id) { 
       caseDATE_DIALOG_ID: 
           returnnew DatePickerDialog(this, mDateSetListener, mYear, mMonth, 
                  mDay);
       caseTIME_DIALOG_ID:
           returnnew TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true);
       }
            
       returnnull
    
   
    @Override 
    protectedvoid onPrepareDialog(intid, Dialog dialog) { 
       switch(id) { 
       caseDATE_DIALOG_ID: 
           ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); 
           break;
       caseTIME_DIALOG_ID:
           ((TimePickerDialog) dialog).updateTime(mHour, mMinute);
           break;
       }
    
   
    /**
     * 處理日期和時間控件的Handler
     */ 
    Handler dateandtimeHandler = newHandler() {
   
       @Override 
       publicvoid handleMessage(Message msg) { 
           switch(msg.what) { 
           caseTimeActivity.SHOW_DATAPICK: 
               showDialog(DATE_DIALOG_ID); 
               break;
           caseTimeActivity.SHOW_TIMEPICK:
               showDialog(TIME_DIALOG_ID);
               break;
           
       
   
    };
     
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章