• Android Video Tutorials

Android - 日期選擇器



Android 日期選擇器允許您在自定義使用者介面中選擇包含日、月、年的日期。為此功能,Android 提供了 DatePicker 和 DatePickerDialog 元件。

在本教程中,我們將演示透過 DatePickerDialog 使用日期選擇器。DatePickerDialog 是一個包含 DatePicker 的簡單對話方塊。

為了顯示 DatePickerDialog,您必須將 DatePickerDialog 的 ID 傳遞給 showDialog(id_of_dialog) 方法。其語法如下:

showDialog(999);

呼叫此 showDialog 方法時,另一個名為 onCreateDialog 的方法會自動呼叫。因此,我們也必須重寫該方法。其語法如下:

@Override
protected Dialog onCreateDialog(int id) {
   // TODO Auto-generated method stub
   if (id == 999) {
      return new DatePickerDialog(this, myDateListener, year, month, day);
   }
   return null;
}

在最後一步,您必須註冊 DatePickerDialog 監聽器並重寫其 onDateSet 方法。此 onDateSet 方法包含更新的日、月和年。其語法如下:

private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
   @Override
   public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
      // arg1 = year
      // arg2 = month
      // arg3 = day		
   }
};
Set Date

除了日期屬性外,DatePicker 物件也傳遞到此函式中。您可以使用 DatePicker 的以下方法執行進一步操作。

序號 方法及描述
1

getDayOfMonth()

此方法獲取選定的月份中的日期

2

getMonth()

此方法獲取選定的月份

3

getYear()

此方法獲取選定的年份

4

setMaxDate(long maxDate)

此方法設定此 DatePicker 支援的最大日期(以自 1970 年 1 月 1 日 00:00:00 以來的毫秒數表示,以 getDefault() 時區為準)

5

setMinDate(long minDate)

此方法設定此 NumberPicker 支援的最小日期(以自 1970 年 1 月 1 日 00:00:00 以來的毫秒數表示,以 getDefault() 時區為準)

6

setSpinnersShown(boolean shown)

此方法設定是否顯示微調器

7

updateDate(int year, int month, int dayOfMonth)

此方法更新當前日期

8

getCalendarView()

此方法返回日曆檢視

9

getFirstDayOfWeek()

此方法返回一週的第一天

示例

這是一個演示 DatePickerDialog 類用法的示例。它建立一個基本的日期選擇器應用程式,允許您使用 DatePicker 小部件設定日期。

要試驗此示例,您可以在實際裝置或模擬器上執行它。

步驟 描述
1 您將使用 Android Studio 建立一個 Android 應用程式,並將其命名為 DatePicker,包名為 com.example.datepicker。
2 修改 src/MainActivity.java 檔案以新增必要的程式碼。
3 修改 res/layout/activity_main 以新增相應的 XML 元件。
4 修改 res/values/string.xml 以新增必要的字串元件。
5 執行應用程式,選擇正在執行的 Android 裝置,將應用程式安裝在其上並驗證結果。

以下是修改後的主活動檔案 src/com.example.datepicker/MainActivity.java 的內容。

package com.example.datepicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;

import android.os.Bundle;

import android.view.Menu;
import android.view.View;

import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
   private DatePicker datePicker;
   private Calendar calendar;
   private TextView dateView;
   private int year, month, day;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      dateView = (TextView) findViewById(R.id.textView3);
      calendar = Calendar.getInstance();
      year = calendar.get(Calendar.YEAR);
      
      month = calendar.get(Calendar.MONTH);
      day = calendar.get(Calendar.DAY_OF_MONTH);
      showDate(year, month+1, day);
   }

   @SuppressWarnings("deprecation")
   public void setDate(View view) {
      showDialog(999);
      Toast.makeText(getApplicationContext(), "ca", 
         Toast.LENGTH_SHORT)
      .show();
   }

   @Override
   protected Dialog onCreateDialog(int id) {
      // TODO Auto-generated method stub
      if (id == 999) {
         return new DatePickerDialog(this, 
            myDateListener, year, month, day);
      }
      return null;
   }

   private DatePickerDialog.OnDateSetListener myDateListener = new 
      DatePickerDialog.OnDateSetListener() {
      @Override
      public void onDateSet(DatePicker arg0, 
         int arg1, int arg2, int arg3) {
         // TODO Auto-generated method stub
         // arg1 = year
         // arg2 = month
         // arg3 = day
         showDate(arg1, arg2+1, arg3);
      }
   };

   private void showDate(int year, int month, int day) {
      dateView.setText(new StringBuilder().append(day).append("/")
      .append(month).append("/").append(year));
   }
}

以下是修改後的 xml 檔案 res/layout/activity_main.xml 的內容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="70dp"
      android:onClick="setDate"
      android:text="@string/date_button_set" />

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="24dp"
      android:text="@string/date_label_set"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/button1"
      android:layout_marginTop="66dp"
      android:layout_toLeftOf="@+id/button1"
      android:text="@string/date_view_set"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/button1"
      android:layout_below="@+id/textView2"
      android:layout_marginTop="72dp"
      android:text="@string/date_selected"
      android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

以下是 res/values/string.xml 的內容。

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">DatePicker</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="date_label_set">Press the button to set the date</string>
   <string name="date_button_set">Set Date</string>
   <string name="date_view_set">The Date is: </string>
   <string name="date_selected"></string>
</resources>

讓我們嘗試執行我們剛剛修改的 DatePicker 應用程式。我假設您在進行環境設定時建立了您的 AVD。要從 Eclipse 執行應用程式,請開啟專案的一個活動檔案,然後單擊工具欄中的執行 Eclipse Run Icon 圖示。Eclipse 會將應用程式安裝到您的 AVD 並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗:

Android Date Picker Tutorial

現在您可以看到日期已在底部標籤中設定。現在,我們將透過按“設定日期”按鈕來透過 DatePickerDialog 更改日期。按下按鈕後,將出現以下螢幕。

Android Date Picker Tutorial

現在設定所需的日期,設定日期後,按“完成”按鈕。此對話方塊將消失,您新設定的日期將開始顯示在螢幕上。如下所示。

Android Date Picker Tutorial
android_user_interface_controls.htm
廣告
© . All rights reserved.