時間日期選擇,對于android開發是非常常見的,如果你讓用戶輸入,不但設計稿不會這么寫,自己也覺得別扭。最近因為有時間選擇的需求,所以就用系統的DatePickerDialog和TimePickerDialog再封裝了一些時間選擇的方法。
DatePickerDialog和TimePickerDialog,兩個都是以對話框的形式出現,并在內部實現了布局(不需要添加任何的布局),最最重要的是調用方法簡單易學,now,let's begin.
日期選擇的基本實現
DatePickerDialog怎么用?可以在構造方法源碼一探究竟
筆者在源碼中數了一下,一共有五個構造方法,先介紹最簡單的:
/**
* @param context The context the dialog is to run in.
* @param callBack How the parent is notified that the date is set.
* @param year The initial year of the dialog.
* @param monthOfYear The initial month of the dialog.
* @param dayOfMonth The initial day of the dialog.
*/
public DatePickerDialog(Context context,
OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth) {
this(context, 0, callBack, year, monthOfYear, dayOfMonth);
}
通過自帶的注釋應該都懂是各個參數是什么了吧,現在可以寫一個封裝的選擇時間的方法了
使用默認主題的DatePickerDialog:
public static void showDatePickerDialog(Activity activity, final TextView tv, Calendar calendar) {
// Calendar 需要這樣來得到
// Calendar calendar = Calendar.getInstance();
// 直接創建一個DatePickerDialog對話框實例,并將它顯示出來
new DatePickerDialog(activity,
// 綁定監聽器(How the parent is notified that the date is set.)
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// 此處得到選擇的時間,可以進行你想要的操作
tv.setText("您選擇了:" + year + "年" + monthOfYear
+ "月" + dayOfMonth + "日");
}
}
// 設置初始日期
, calendar.get(Calendar.YEAR)
,calendar.get(Calendar.MONTH)
,calendar.get(Calendar.DAY_OF_MONTH)).show();
}
運行結果見最下面的運行圖
下面就來挨個看看他的構造方法,注釋很詳細,不外乎就是那幾個參數,如果傳入就用傳入的,沒有就用默認的:
第一個:
/**
* Creates a new date picker dialog for the current date using the parent
* context's default date picker dialog theme.
*
* @param context the parent context
*/
public DatePickerDialog(@NonNull Context context) {
this(context, 0, null, Calendar.getInstance(), -1, -1, -1);
}
第二個:
/**
* Creates a new date picker dialog for the current date.
*
* @param context the parent context
* @param themeResId the resource ID of the theme against which to inflate
* this dialog, or {@code 0} to use the parent
* {@code context}'s default alert dialog theme
*/
public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId) {
this(context, themeResId, null, Calendar.getInstance(), -1, -1, -1);
}
第三個:
/**
* Creates a new date picker dialog for the specified date.
*
* @param context the parent context
* @param themeResId the resource ID of the theme against which to inflate
* this dialog, or {@code 0} to use the parent
* {@code context}'s default alert dialog theme
* @param listener the listener to call when the user sets the date
* @param year the initially selected year
* @param monthOfYear the initially selected month of the year (0-11 for
* compatibility with {@link Calendar#MONTH})
* @param dayOfMonth the initially selected day of month (1-31, depending
* on month)
*/
public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId,
@Nullable OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
this(context, themeResId, listener, null, year, monthOfYear, dayOfMonth);
}
因為最后一個構造方法是private的,所以就補貼出來了;
現在 我們用第三個來指定主題 (敲黑板),以下常量:
而筆者親測>=5或者<=0時就會使用默認主題,所以上述常量是沒有5的!?。?!
改起來也是非常簡單的,直接添加一個參數就ok,直接上代碼咯
public static void showDatePickerDialog(Activity activity, int themeResId, final TextView tv, Calendar calendar) {
// 直接創建一個DatePickerDialog對話框實例,并將它顯示出來
new DatePickerDialog(activity
, themeResId
// 綁定監聽器(How the parent is notified that the date is set.)
,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// 此處得到選擇的時間,可以進行你想要的操作
tv.setText("您選擇了:" + year + "年" + monthOfYear
+ "月" + dayOfMonth + "日");
}
}
// 設置初始日期
, calendar.get(Calendar.YEAR)
,calendar.get(Calendar.MONTH)
,calendar.get(Calendar.DAY_OF_MONTH)).show();
}
時間選擇的基礎實現
TimePickerDialog構造方法源碼一探究竟
/**
* Creates a new time picker dialog.
*
* @param context the parent context
* @param listener the listener to call when the time is set
* @param hourOfDay the initial hour
* @param minute the initial minute
* @param is24HourView whether this is a 24 hour view or AM/PM
*/
public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,
boolean is24HourView) {
this(context, 0, listener, hourOfDay, minute, is24HourView);
}
動手封裝一下吧
public static void showTimePickerDialog(Activity activity, final TextView tv, Calendar calendar) {
// Calendar c = Calendar.getInstance();
// 創建一個TimePickerDialog實例,并把它顯示出來
// 解釋一哈,Activity是context的子類
new TimePickerDialog( activity,
// 綁定監聽器
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view,
int hourOfDay, int minute) {
tv.setText("您選擇了:" + hourOfDay + "時" + minute
+ "分");
}
}
// 設置初始時間
, calendar.get(Calendar.HOUR_OF_DAY)
, calendar.get(Calendar.MINUTE)
// true表示采用24小時制
,true).show();
}
同樣,日期有兩個構造方法,另一個可以設置主題
/**
* Creates a new time picker dialog with the specified theme.
* <p>
* The theme is overlaid on top of the theme of the parent {@code context}.
* If {@code themeResId} is 0, the dialog will be inflated using the theme
* specified by the
* {@link android.R.attr#timePickerDialogTheme android:timePickerDialogTheme}
* attribute on the parent {@code context}'s theme.
*
* @param context the parent context
* @param themeResId the resource ID of the theme to apply to this dialog
* @param listener the listener to call when the time is set
* @param hourOfDay the initial hour
* @param minute the initial minute
* @param is24HourView Whether this is a 24 hour view, or AM/PM.
*/
public TimePickerDialog(Context context, int themeResId, OnTimeSetListener listener,
int hourOfDay, int minute, boolean is24HourView) {...}
theme :
THEME_TRADITIONAL
- The traditional (pre-Holo) alert dialog theme (int 1)
THEME_HOLO_DARK
- The holographic alert theme with a dark background (int 2)
THEME_HOLO_LIGHT
- The holographic alert theme with a light background (int 3)
THEME_DEVICE_DEFAULT_DARK
- The device's default alert theme with a dark background. (int 4)
THEME_DEVICE_DEFAULT_LIGHT
- The device's default alert theme with a light background. (int 5)
上面已經改過,這么簡單就不貼代碼了
而筆者親測>=5或者<=0時就會使用默認主題,所以上述常量也是沒有5的?。。?!

TimePickerDialog.THEME_HOLO_DARK之類的手法可以得到對應的int值