DatePicker日期滾動(dòng)選擇使用詳解
本文實(shí)例為大家分享了DatePicker日期滾動(dòng)選擇的使用,供大家參考,具體內(nèi)容如下
效果圖為:
1.dialog_date.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" ? ? android:orientation="vertical"> ? ? <LinearLayout ? ? ? ? android:orientation="horizontal" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginTop="10dp" ? ? ? ? android:background="@color/background"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tv_cancle" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_centerVertical="true" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:padding="10dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="@string/cancle" ? ? ? ? ? ? android:textColor="@color/colorBlack" ? ? ? ? ? ? android:textSize="16sp" /> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_weight="3" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_centerInParent="true" /> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tv_ok" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:padding="10dp" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_centerVertical="true" ? ? ? ? ? ? android:text="@string/commit" ? ? ? ? ? ? android:textColor="@color/colorBlack" ? ? ? ? ? ? android:textSize="16sp" /> ? ? </LinearLayout> ? ? <DatePicker ? ? ? ? android:id="@+id/datepicker" ? ? ? ? android:datePickerMode="spinner" ? ? ? ? android:calendarViewShown="false" ? ? ? ? android:startYear="2017" ? ? ? ? android:endYear="2020" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" /> </LinearLayout>
布局里看起來(lái)還不是滾動(dòng)式的,但所有工作弄完后,就是滾動(dòng)啦
2.對(duì)應(yīng)的MyDatePicker類:
public class MyDatePicker implements DatePicker.OnDateChangedListener, ? ? ? ? TimePicker.OnTimeChangedListener { ? ? /** ? ? ?* 定義結(jié)果回調(diào)接口 ? ? ?*/ ? ? public interface ResultHandler { ? ? ? ? void handle(String time); ? ? } ? ? private DatePicker datePicker; ? ? private TextView tv_ok; ? ? private TextView tv_cancle; ? ? private ResultHandler handler; ? ? private String dateTime; ? ? private Context context; ? ? private String initDateTime; ? ? private Dialog datePickerDialog; ? ? public MyDatePicker(Context context, ResultHandler resultHandler, String initDateTime) { ? ? ? ? this.context = context; ? ? ? ? this.handler = resultHandler; ? ? ? ? this.initDateTime = initDateTime; ? ? ? ? initDialog(); ? ? } ? ? private void initDialog() { ? ? ? ? if (datePickerDialog == null) { ? ? ? ? ? ? datePickerDialog = new Dialog(context, R.style.mytime_dialog); // ? ? ? ? ? ?datePickerDialog = new Dialog(context); ? ? ? ? ? ? datePickerDialog.setCancelable(false); ? ? ? ? ? ? datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); ? ? ? ? ? ? datePickerDialog.setContentView(R.layout.dialog_date); ? ? ? ? ? ? Window window = datePickerDialog.getWindow(); ? ? ? ? ? ? window.setGravity(Gravity.BOTTOM); ? ? ? ? ? ? window.setWindowAnimations(R.style.dialogWindowAnim); //設(shè)置窗口彈出動(dòng)畫(huà) ? ? ? ? ? ? WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); ? ? ? ? ? ? DisplayMetrics dm = new DisplayMetrics(); ? ? ? ? ? ? manager.getDefaultDisplay().getMetrics(dm); ? ? ? ? ? ? WindowManager.LayoutParams lp = window.getAttributes(); ? ? ? ? ? ? lp.width = dm.widthPixels; ? ? ? ? ? ? window.setAttributes(lp); ? ? ? ? } ? ? ? ? initView(); ? ? } ? ? private void initView() { ? ? ? ? datePicker = (DatePicker) datePickerDialog.findViewById(R.id.datepicker); ? ? ? ? tv_ok = (TextView) datePickerDialog.findViewById(R.id.tv_ok); ? ? ? ? tv_cancle = (TextView) datePickerDialog.findViewById(R.id.tv_cancle); ? ? ? ? tv_cancle.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? datePickerDialog.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? tv_ok.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? handler.handle( dateTime ); ? ? ? ? ? ? ? ? datePickerDialog.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? datePickerDialog.show(); ? ? ? ? initDate(datePicker); ? ? } ? ? public void initDate(DatePicker datePicker) { ? ? ? ? Calendar calendar = Calendar.getInstance(); ? ? ? ? if (!(null == initDateTime || "".equals(initDateTime))) { ? ? ? ? ? ? calendar = this.getCalendarByInintData(initDateTime); ? ? ? ? } else { ? ? ? ? ? ? initDateTime = calendar.get(Calendar.YEAR) + "年" ? ? ? ? ? ? ? ? ? ? + calendar.get(Calendar.MONTH) + "月" ? ? ? ? ? ? ? ? ? ? + calendar.get(Calendar.DAY_OF_MONTH) + "日 " ? ? ? ? ? ? ? ? ? ? + calendar.get(Calendar.HOUR_OF_DAY) + ":" ? ? ? ? ? ? ? ? ? ? + calendar.get(Calendar.MINUTE); ? ? ? ? } ? ? ? ? datePicker.init(calendar.get(Calendar.YEAR), ? ? ? ? ? ? ? ? calendar.get(Calendar.MONTH), ? ? ? ? ? ? ? ? calendar.get(Calendar.DAY_OF_MONTH), this); ? ? } ? ? /** ? ? ?* 實(shí)現(xiàn)將初始日期時(shí)間2012年07月02日 16:45 拆分成年 月 日 時(shí) 分 秒,并賦值給calendar ? ? ?* ? ? ?* @param initDateTime ? ? ?* ? ? ? ? ? ?初始日期時(shí)間值 字符串型 ? ? ?* @return Calendar ? ? ?*/ ? ? private Calendar getCalendarByInintData(String initDateTime) { ? ? ? ? Calendar calendar = Calendar.getInstance(); ? ? ? ? // 將初始日期時(shí)間2012年07月02日 16:45 拆分成年 月 日 時(shí) 分 秒 ? ? ? ? String date = spliteString(initDateTime, "日", "index", "front"); // 日期 ? ? ? ? String time = spliteString(initDateTime, "日", "index", "back"); // 時(shí)間 ? ? ? ? String yearStr = spliteString(date, "年", "index", "front"); // 年份 ? ? ? ? String monthAndDay = spliteString(date, "年", "index", "back"); // 月日 ? ? ? ? String monthStr = spliteString(monthAndDay, "月", "index", "front"); // 月 ? ? ? ? String dayStr = spliteString(monthAndDay, "月", "index", "back"); // 日 ? ? ? ? String hourStr = spliteString(time, ":", "index", "front"); // 時(shí) ? ? ? ? String minuteStr = spliteString(time, ":", "index", "back"); // 分 ? ? ? ? int currentYear = Integer.valueOf(yearStr.trim()).intValue(); ? ? ? ? int currentMonth = Integer.valueOf(monthStr.trim()).intValue() - 1; ? ? ? ? int currentDay = Integer.valueOf(dayStr.trim()).intValue(); ? ? ? ? int currentHour = Integer.valueOf(hourStr.trim()).intValue(); ? ? ? ? int currentMinute = Integer.valueOf(minuteStr.trim()).intValue(); ? ? ? ? calendar.set(currentYear, currentMonth, currentDay, currentHour, ? ? ? ? ? ? ? ? currentMinute); ? ? ? ? return calendar; ? ? } ? ? /** ? ? ?* 截取子串 ? ? ?* ? ? ?* @param srcStr ? ? ?* ? ? ? ? ? ?源串 ? ? ?* @param pattern ? ? ?* ? ? ? ? ? ?匹配模式 ? ? ?* @param indexOrLast ? ? ?* @param frontOrBack ? ? ?* @return ? ? ?*/ ? ? public static String spliteString(String srcStr, String pattern, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String indexOrLast, String frontOrBack) { ? ? ? ? String result = ""; ? ? ? ? int loc = -1; ? ? ? ? if (indexOrLast.equalsIgnoreCase("index")) { ? ? ? ? ? ? loc = srcStr.indexOf(pattern); // 取得字符串第一次出現(xiàn)的位置 ? ? ? ? } else { ? ? ? ? ? ? loc = srcStr.lastIndexOf(pattern); // 最后一個(gè)匹配串的位置 ? ? ? ? } ? ? ? ? if (frontOrBack.equalsIgnoreCase("front")) { ? ? ? ? ? ? if (loc != -1) ? ? ? ? ? ? ? ? result = srcStr.substring(0, loc); // 截取子串 ? ? ? ? } else { ? ? ? ? ? ? if (loc != -1) ? ? ? ? ? ? ? ? result = srcStr.substring(loc + 1, srcStr.length()); // 截取子串 ? ? ? ? } ? ? ? ? return result; ? ? } ? ? @Override ? ? public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) { ? ? ? ? // 獲得日歷實(shí)例 ? ? ? ? Calendar calendar = Calendar.getInstance(); ? ? ? ? calendar.set(datePicker.getYear(), datePicker.getMonth(), ? ? ? ? ? ? ? ? datePicker.getDayOfMonth()); ? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); ? ? ? ? dateTime = sdf.format(calendar.getTime()); ? ? } ? ? @Override ? ? public void onTimeChanged(TimePicker timePicker, int i, int i1) { ? ? ? ? onDateChanged(null, 0, 0, 0); ? ? } }
設(shè)置對(duì)話框樣式,核心代碼:
private void initDialog() { ? ? ? ? if (datePickerDialog == null) { ? ? ? ? ? ? datePickerDialog = new Dialog(context, R.style.mytime_dialog); // ? ? ? ? ? ?datePickerDialog = new Dialog(context); ? ? ? ? ? ? datePickerDialog.setCancelable(false); ? ? ? ? ? ? datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); ? ? ? ? ? ? datePickerDialog.setContentView(R.layout.dialog_date); ? ? ? ? ? ? Window window = datePickerDialog.getWindow(); ? ? ? ? ? ? window.setGravity(Gravity.BOTTOM);//使對(duì)話框出現(xiàn)在底部 ? ? ? ? ? ? window.setWindowAnimations(R.style.dialogWindowAnim); //設(shè)置窗口彈出動(dòng)畫(huà) ? ? ? ? ? ? WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); ? ? ? ? ? ? DisplayMetrics dm = new DisplayMetrics(); ? ? ? ? ? ? manager.getDefaultDisplay().getMetrics(dm); ? ? ? ? ? ? WindowManager.LayoutParams lp = window.getAttributes(); ? ? ? ? ? ? lp.width = dm.widthPixels; ? ? ? ? ? ? window.setAttributes(lp); ? ? ? ? } ? ? ? ? initView(); ? ? }
定義對(duì)話框style風(fēng)格和添加動(dòng)畫(huà):
<style name="mytime_dialog" ?parent="Theme.AppCompat.Light.NoActionBar"> ? ? ? ? <item name="android:windowFrame">@null</item> ? ? ? ? <item name="android:windowNoTitle">true</item> ? ? ? ? <item name="android:windowIsFloating">true</item> ? ? ? ? <item name="android:windowContentOverlay">@null</item> ? ? ? ? <item name="android:windowBackground">@color/background</item> ? ? </style> ? ? <style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1"> ? ? ? ? <item name="android:windowEnterAnimation">@anim/dialog_enter_anim</item> ? ? ? ? <item name="android:windowExitAnimation">@anim/dialog_exit_anim</item> </style>
dialog_enter_anim.xml的代碼:
<?xml version="1.0" encoding="utf-8"?> <!-- 彈出時(shí)動(dòng)畫(huà) --> <set xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <scale ? ? ? ? android:interpolator="@android:anim/accelerate_interpolator" ? ? ? ? android:fromXScale="1.0" ? ? ? ? android:toXScale="1.0" ? ? ? ? android:fromYScale="0.0" ? ? ? ? android:toYScale="1.0" ? ? ? ? android:pivotX="0%" ? ? ? ? android:pivotY="100%" ? ? ? ? android:fillAfter="false" ? ? ? ? android:duration="400"/> </set>
dialog_exit_anim.xml的代碼:
<?xml version="1.0" encoding="utf-8"?> <!-- 退出時(shí)動(dòng)畫(huà)效果 --> <set xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <scale ? ? ? ? android:interpolator="@android:anim/accelerate_interpolator" ? ? ? ? android:fromXScale="1.0" ? ? ? ? android:toXScale="1.0" ? ? ? ? android:fromYScale="1.0" ? ? ? ? android:toYScale="0.0" ? ? ? ? android:pivotX="0%" ? ? ? ? android:pivotY="100%" ? ? ? ? android:fillAfter="false" ? ? ? ? android:duration="400"/> </set>
3.在主界面activity中需要使用日期對(duì)話框的地方,調(diào)用函數(shù)initMyDatePicker()即可:
private void initMyDatePicker() { ? ? ? ? SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 ? ?HH:mm");//格式為 2013年9月3日 14:44 ? ? ? ? Date curDate = new Date(System.currentTimeMillis());//獲取當(dāng)前時(shí)間 ? ? ? ? String currentDate = formatter.format(curDate); ? ? ? ? myDatePicker = new MyDatePicker(this, new MyDatePicker.ResultHandler() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void handle(String time) { ? ? ? ? ? ? ? ? tv_showCurrentDate1.setText(time ); ? ? ? ? ? ? } ? ? ? ? },currentDate); ? ? }
注:DatePicker的樣式會(huì)受主題的樣式影響,寫(xiě)的時(shí)候弄了好久,一定要注意才行
<style name="mytime_dialog" ?parent="Theme.AppCompat.Light.NoActionBar"> ? ? ? ? <item name="android:windowFrame">@null</item> ? ? ? ? <item name="android:windowNoTitle">true</item> ? ? ? ? <item name="android:windowIsFloating">true</item> ? ? ? ? <item name="android:windowContentOverlay">@null</item> ? ? ? ? <item name="android:windowBackground">@color/background</item> </style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開(kāi)發(fā)兩個(gè)activity之間傳值示例詳解
這篇文章主要為大家介紹了Android開(kāi)發(fā)兩個(gè)activity之間傳值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Android自定義View之自定義評(píng)價(jià)打分控件RatingBar實(shí)現(xiàn)自定義星星大小和間距
Android開(kāi)發(fā)中,我們經(jīng)常會(huì)用到對(duì)商家或者商品的評(píng)價(jià),運(yùn)用星星進(jìn)行打分。這篇文章介紹了Android自定義View之自定義評(píng)價(jià)打分控件RatingBar可以自定義星星大小和間距的相關(guān)資料,感興趣的朋友一起看看吧2016-10-10android中可以通過(guò)兩種方式調(diào)用接口發(fā)送短信
調(diào)用系統(tǒng)短信接口直接發(fā)送短信;調(diào)起系統(tǒng)發(fā)短信功能,本文將給出兩種方式的實(shí)現(xiàn)代碼,感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02AlertDialog點(diǎn)擊按鈕不消失的實(shí)現(xiàn)方法
我有一個(gè)文本輸入對(duì)話框,當(dāng)我點(diǎn)擊對(duì)話框上的“是”按鈕,它會(huì)驗(yàn)證輸入,然后關(guān)閉對(duì)話框,但是,如果輸入錯(cuò)誤,我想停留在同一個(gè)對(duì)話框中。怎么實(shí)現(xiàn)此功能呢?下面通過(guò)本文給大家分享下2017-01-01Android圖像切換器imageSwitcher的實(shí)例應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android圖像切換器imageSwitcher的實(shí)例應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10Flutter持久化存儲(chǔ)之?dāng)?shù)據(jù)庫(kù)存儲(chǔ)(sqflite)詳解
這篇文章主要給大家介紹了關(guān)于Flutter持久化存儲(chǔ)之?dāng)?shù)據(jù)庫(kù)存儲(chǔ)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Android Walker登錄記住密碼頁(yè)面功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android Walker登錄記住密碼頁(yè)面功能的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05