歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中日期和時間控件的使用

Android中日期和時間控件的使用

日期:2017/3/1 10:29:26   编辑:Linux編程
本文主要講述Android中的日期控件和時間控件的使用,以一個Demo的例子來展示日期和時間控件的使用,先看下如下效果圖:

從效果圖中可以看到該Demo是通過單擊【選擇日期】按鈕和【選擇時間】按鈕彈出日期或者時間的對話框,然後設置日期或者時間,設置完成後會在文本框中顯示設置的日期或時間值。

【1】Demo程序框架圖:

【2】布局文件 res/layout/main.xml 源碼:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:layout_width="fill_parent" android:layout_height="wrap_content"
  8. android:gravity="center" android:text="歡迎關注Andy.Chen Blog" />
  9. <TextView
  10. android:layout_width="fill_parent" android:layout_height="wrap_content"
  11. android:gravity="center" android:text="日期和時間控件的使用DEMO" />
  12. <LinearLayout android:orientation="horizontal"
  13. android:layout_width="fill_parent" android:layout_height="wrap_content">
  14. <EditText android:id="@+id/showdate" android:layout_width="fill_parent"
  15. android:layout_height="wrap_content" android:layout_weight="1"/>
  16. <Button android:id="@+id/pickdate" android:layout_width="wrap_content"
  17. android:layout_height="wrap_content" android:text="選擇日期"/>
  18. </LinearLayout>
  19. <LinearLayout android:orientation="horizontal"
  20. android:layout_width="fill_parent" android:layout_height="wrap_content">
  21. <EditText android:id="@+id/showtime" android:layout_width="fill_parent"
  22. android:layout_height="wrap_content" android:layout_weight="1"/>
  23. <Button android:id="@+id/picktime" android:layout_width="wrap_content"
  24. android:layout_height="wrap_content" android:text="選擇時間"/>
  25. </LinearLayout>
  26. </LinearLayout>

【3】包com.andyidea.calenderdemo下MainActivity.java源碼:

  1. package com.andyidea.calenderdemo;
  2. import java.util.Calendar;
  3. import android.app.Activity;
  4. import android.app.DatePickerDialog;
  5. import android.app.Dialog;
  6. import android.app.TimePickerDialog;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.DatePicker;
  13. import android.widget.EditText;
  14. import android.widget.TimePicker;
  15. public class MainActivity extends Activity {
  16. private EditText showDate = null;
  17. private Button pickDate = null;
  18. private EditText showTime = null;
  19. private Button pickTime = null;
  20. private static final int SHOW_DATAPICK = 0;
  21. private static final int DATE_DIALOG_ID = 1;
  22. private static final int SHOW_TIMEPICK = 2;
  23. private static final int TIME_DIALOG_ID = 3;
  24. private int mYear;
  25. private int mMonth;
  26. private int mDay;
  27. private int mHour;
  28. private int mMinute;
  29. /** Called when the activity is first created. */
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.main);
  34. initializeViews();
  35. final Calendar c = Calendar.getInstance();
  36. mYear = c.get(Calendar.YEAR);
  37. mMonth = c.get(Calendar.MONTH);
  38. mDay = c.get(Calendar.DAY_OF_MONTH);
  39. mHour = c.get(Calendar.HOUR_OF_DAY);
  40. mMinute = c.get(Calendar.MINUTE);
  41. setDateTime();
  42. setTimeOfDay();
  43. }
  44. /**
  45. * 初始化控件和UI視圖
  46. */
  47. private void initializeViews(){
  48. showDate = (EditText) findViewById(R.id.showdate);
  49. pickDate = (Button) findViewById(R.id.pickdate);
  50. showTime = (EditText)findViewById(R.id.showtime);
  51. pickTime = (Button)findViewById(R.id.picktime);
  52. pickDate.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. Message msg = new Message();
  56. if (pickDate.equals((Button) v)) {
  57. msg.what = MainActivity.SHOW_DATAPICK;
  58. }
  59. MainActivity.this.dateandtimeHandler.sendMessage(msg);
  60. }
  61. });
  62. pickTime.setOnClickListener(new View.OnClickListener() {
  63. @Override
  64. public void onClick(View v) {
  65. Message msg = new Message();
  66. if (pickTime.equals((Button) v)) {
  67. msg.what = MainActivity.SHOW_TIMEPICK;
  68. }
  69. MainActivity.this.dateandtimeHandler.sendMessage(msg);
  70. }
  71. });
  72. }
  73. /**
  74. * 設置日期
  75. */
  76. private void setDateTime(){
  77. final Calendar c = Calendar.getInstance();
  78. mYear = c.get(Calendar.YEAR);
  79. mMonth = c.get(Calendar.MONTH);
  80. mDay = c.get(Calendar.DAY_OF_MONTH);
  81. updateDateDisplay();
  82. }
  83. /**
  84. * 更新日期顯示
  85. */
  86. private void updateDateDisplay(){
  87. showDate.setText(new StringBuilder().append(mYear).append("-")
  88. .append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-")
  89. .append((mDay < 10) ? "0" + mDay : mDay));
  90. }
  91. /**
  92. * 日期控件的事件
  93. */
  94. private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
  95. public void onDateSet(DatePicker view, int year, int monthOfYear,
  96. int dayOfMonth) {
  97. mYear = year;
  98. mMonth = monthOfYear;
  99. mDay = dayOfMonth;
  100. updateDateDisplay();
  101. }
  102. };
  103. /**
  104. * 設置時間
  105. */
  106. private void setTimeOfDay(){
  107. final Calendar c = Calendar.getInstance();
  108. mHour = c.get(Calendar.HOUR_OF_DAY);
  109. mMinute = c.get(Calendar.MINUTE);
  110. updateTimeDisplay();
  111. }
  112. /**
  113. * 更新時間顯示
  114. */
  115. private void updateTimeDisplay(){
  116. showTime.setText(new StringBuilder().append(mHour).append(":")
  117. .append((mMinute < 10) ? "0" + mMinute : mMinute));
  118. }
  119. /**
  120. * 時間控件事件
  121. */
  122. private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
  123. @Override
  124. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  125. mHour = hourOfDay;
  126. mMinute = minute;
  127. updateTimeDisplay();
  128. }
  129. };
  130. @Override
  131. protected Dialog onCreateDialog(int id) {
  132. switch (id) {
  133. case DATE_DIALOG_ID:
  134. return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
  135. mDay);
  136. case TIME_DIALOG_ID:
  137. return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true);
  138. }
  139. return null;
  140. }
  141. @Override
  142. protected void onPrepareDialog(int id, Dialog dialog) {
  143. switch (id) {
  144. case DATE_DIALOG_ID:
  145. ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
  146. break;
  147. case TIME_DIALOG_ID:
  148. ((TimePickerDialog) dialog).updateTime(mHour, mMinute);
  149. break;
  150. }
  151. }
  152. /**
  153. * 處理日期和時間控件的Handler
  154. */
  155. Handler dateandtimeHandler = new Handler() {
  156. @Override
  157. public void handleMessage(Message msg) {
  158. switch (msg.what) {
  159. case MainActivity.SHOW_DATAPICK:
  160. showDialog(DATE_DIALOG_ID);
  161. break;
  162. case MainActivity.SHOW_TIMEPICK:
  163. showDialog(TIME_DIALOG_ID);
  164. break;
  165. }
  166. }
  167. };
  168. }

【4】程序運行效果圖:

Copyright © Linux教程網 All Rights Reserved