可以用DatePickerDialog顯示選取日期的對話框。可以設置顯示的樣式
1、通過構造方法設置顯示樣式。
可以通過DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)這個構造方法的第二個參數來設置顯示樣式。
這個theme參數對應的值,可以使用AlertDialog中的theme值。
AlertDialog.THEME_TRADITIONAL
AlertDialog.THEME_HOLO_DARK
AlertDialog.THEME_HOLO_LIGHT
AlertDialog.THEME_DEVICE_DEFAULT_DARK
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT
2、通過DatePicker設置顯示樣式
首先獲取DatePicker,然后使用DatePicker.setCalendarViewShow(boolean)和DatePicker.setSpinnersShow(boolean)來設置。
CalendarView和Spinners的值分別為true和false
CalendarView和Spinners的值分別為false和true
CalendarView和Spinners的值都是false
CalendarView和Spinners的值都是true
對於TimePickerDialog而言,它的樣式設置,只有構造函數一種方式,對應的theme參數和DatePickerDialog相同。它內部定義了一個TimePicker,但是沒有提供獲取的方式。
在構造TimePickerDialog和DatePickerDialog的時候最好使用DialogFragment來進行構造。
幫助文檔中例子:
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 } }
public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePickerFragment(); newFragment.show(getSupportFragmentManager(), "timePicker"); }
對於DialogFragment可以參考文檔: Android 官方推薦 : DialogFragment 創建對話框