概述
安卓提供了現成的對話框,讓用戶選擇一個時間或日期。每一個選擇器控制時間(小時,分鍾,AM/PM)或日期(月,日,年)的每一部分的選擇。使用這些選擇器幫助 確保用戶正確的,格式化的,和適合的選擇一個時間或日期。
我們推薦你使用
DialogFragment
來容納每一個時間或日期選擇器。
DialogFragment
為你管理對話框的生命周期並且允許你在不同的布局結構中顯示該選擇器,例如 手機中的基本對話框或在大屏幕中作為一部分嵌入。
盡管
DialogFragment是在安卓3.0(API 11)才第一次出現,如果你的應用支持3.0更早的版本——即使是安卓1.6——你可以在
support library使用
DialogFragment 來向后兼容。
注:下面是示例代碼展示了為
DialogFragment使用
support library API來如何創建一個時間選擇器和日期選擇器。如果你的應用
minSdkVersion 是11或之上,你可以直接使用
DialogFragment
.。
創建一個時間選擇器
使用
DialogFragment來展示一個時間選擇對話框(
TimePickerDialog),你需要定義一個繼承自
DialogFragment的fragment 類,並且從該fragment的
onCreateDialog()方法中返回一個
TimePickerDialog。
注:如果你的應用支持的版本為比安卓3.0更老,確保你的安卓工程使用了support library
將 DialogFragment延伸為時間選擇器(time picker)
為一個
TimePickerDialog 定義一個
DialogFragment
,你必須:
- 定義
onCreateDialog()
來返回一個TimePickerDialog的引用 - 實現
TimePickerDialog.OnTimeSetListener
接口來響應用戶設置時間的事件
這里有個例子:
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
查看
TimePickerDialog 類來了解更多關於該構造函數參數的信息
現在你所需要做的就是將這個fragment的實例添加到你的activity中。
展示時間選擇器
一個例子,這里有一個按鈕,當點擊時,調用一個方法來顯示該對話框:
<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");
}
警告:如果你的應用支持安卓3.0及其以下版本,確保你在調用
getSupportFragmentManager()
時獲得了一個
FragmentManager
.的引用。也應該確保你展示該時間選擇器的activity繼承了
FragmentActivity而不是標准的
Activity
類。
創建日期選擇器(Date Picker)
創建日期選擇對話框(
DatePickerDialog)和創建時間選擇對話框(
TimePickerDialog
.)相似。唯一的不同將在於為對話框創建的fragment。
為使用
DialogFragment來展示一個
DatePickerDialog
,你需要定義一個繼承自
DialogFragment
的fragment類並且從fragment中的
onCreateDialog()方法中返回一個
DatePickerDialog
注:如果你的應用支撐安卓3.0及更早的版本,確保你的工程中使用了安卓 support library
將DialogFragment 延伸為日期選擇器(date picker)
為
DatePickerDialog定義一個
DialogFragment,你必須:
- 定義一個
onCreateDialog()
方法返回一個DatePickerDialog的引用 - 實現DatePickerDialog.OnDateSetListener接口來接收用戶選擇日期的事件
下面是個示例:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
查看
DatePickerDialog類來了解關於它構造函數參數的消息。
現在你只需要將這個fragment實例添加到你的activity中去了。
展示日期選擇器
例如,這里有個按鈕,當點擊它時,調用一個方法來展示該對話框:
<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");
}
警告:如果你的應用支持安卓3.0及其以下版本,確保你在調用
getSupportFragmentManager()
時獲得了一個
FragmentManager
.的引用。也應該確保你展示該時間選擇器的activity繼承了
FragmentActivity而不是標准的
Activity
類。