選擇器
Android爲用戶提供了現成的選擇時間或日期的對話框控件。每種選擇器提供了可選擇每一部分時間(時,分,上午/下午)或日期(月,日,年)的控件。使用這些選擇器有助於確保用戶可以選擇有效的、格式正確的且適用於用戶所在地區的時間或日期。
我們建議你使用 DialogFragment
來承載每個時間或日期選擇器。DialogFragment
可以爲你管理對話框的生命週期,並且可以以不同的佈局配置顯示選擇器,例如在手機上時顯示在基本對話框中,在大屏幕上時作爲佈局嵌入的一部分顯示。
雖然 DialogFragment
直到Android 3.0
(API等級11)才被引入,但是如果你的應用支持Android 3.0以下的版本,甚至是Android 1.6版本,你仍然可以使用 support library 中獲取的 DialogFragment 類來保證向後兼容。
註解:下面代碼示例說明如何使用 support library APIS獲取 DialogFragment 來爲時間或日期選擇器創建對話框。如果應用的 minSdkVersion
是11或更高的話,你可以直接使用平臺提供的 DialogFragment。
創建時間選擇器
爲了通過使用 DialogFragment 來顯示 TimePickerDialog,你需要定義一個fragment類,它繼承自 DialogFragment 且通過fragment的 onCreateDialog() 方法返回 TimePickerDialog。
註解:如果你的應用支持Android 3.0以下的版本,請確保你的Android工程按照 Setting Up a Project to Use a Library 設置了支持庫。
繼承DialogFragment,創建時間選擇器
要爲 TimePickerDialog 定義一個 DialogFragment
的話,你必須:
- 定義
onCreateDialog()
方法並返回TimePickerDialog
的實例 - 實現
TimePickerDialog.OnTimeSetListener
接口,以便用戶設置時間時接收回調
下面是一個例子:
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// 使用當前時間作爲選擇器的默認值
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// 創建TimePickerDialog實例並返回
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// 用戶選擇時間後執行
}
}
請查閱 TimePickerDialog
類瞭解更多關於構造方法參數的信息。
現在需要做的就是將該fragment的實例添加到你的activity中。
顯示時間選擇器
一旦你定義了一個如上所示的 DialogFragment
,就可以通過實例化 DialogFragment 並調用 show() 來顯示時間選擇器。
例如,這裏有個按鈕,當點擊的時候來調用該方法顯示對話框:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_time"
android:onClick="showTimePickerDialog" />
當用戶點擊這個按鈕時,系統會調用下面的方法:
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
這個方法調用上面定義的 DialogFragment 實例的 show()。show()
方法需要 FragmentManager
的實例和這個fragment的唯一標籤名。
註解:如果你的應用支持Android 3.0一下的版本,請確保你調用 getSupportFragmentManager() 來獲取 FragmentManager 實例。同時確保顯示時間選擇器的activity繼承了 FragmentActivity 而不是 Activity。
創建日期選擇器
創建 DatePickerDialog
與創建 TimePickerDialog
過程類似。唯一的區別是爲fragment創建的對話框不同。
爲了通過使用 DialogFragment 來顯示 DatePickerDialog,你需要定義一個fragment類,它繼承自 DialogFragment 且通過fragment的 onCreateDialog() 方法返回 DatePickerDialog。
註解:如果你的應用支持Android 3.0以下的版本,請確保你的Android工程按照 Setting Up a Project to Use a Library 設置了支持庫。
繼承DialogFragment,創建日期選擇器
要爲 DatePickerDialog 定義一個 DialogFragment
的話,你必須:
- 定義
onCreateDialog()
方法並返回 DatePickerDialog -
實現
DatePickerDialog.OnDateSetListener
接口,以便用戶設置日期時接收回調
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// 使用當前日期作爲選擇器的默認日期
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// 創建DatePickerDialog的實例並返回
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// 用戶選擇日期後執行
}
}
請查閱 DatePickerDialog 類瞭解更多關於構造方法參數的信息。
現在需要做的就是將該fragment的實例添加到你的activity中。
顯示日期選擇器
一旦你定義了一個如上所示的 DialogFragment
,就可以通過實例化 DialogFragment 並調用 show() 來顯示時間選擇器。
例如,這裏有個按鈕,當點擊的時候來調用該方法顯示對話框:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_date"
android:onClick="showDatePickerDialog" />
當用戶點擊這個按鈕時,系統會調用下面的方法:
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
這個方法調用上面定義的 DialogFragment 實例的 show()。show()
方法需要 FragmentManager
的實例和這個fragment的唯一標籤名。
註解:如果你的應用支持Android 3.0一下的版本,請確保你調用 getSupportFragmentManager() 來獲取 FragmentManager 實例。同時確保顯示時間選擇器的activity繼承了 FragmentActivity 而不是 Activity。