網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(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)畫
? ? ? ? ? ? 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)畫
? ? ? ? ? ? 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)畫:
<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)畫 --> <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)畫效果 --> <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ì)受主題的樣式影響,寫的時(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>
原文鏈接:https://blog.csdn.net/An_nAl/article/details/77069472
相關(guān)推薦
- 2022-07-28 Android?EventBus粘性事件實(shí)現(xiàn)機(jī)制探究_Android
- 2022-05-05 Android開(kāi)發(fā)之自定義加載動(dòng)畫詳解_Android
- 2022-12-10 React?useCallback詳細(xì)使用教程_React
- 2022-10-06 Redis位圖bitmap操作_Redis
- 2022-07-22 實(shí)現(xiàn)自定義HTTP服務(wù)器
- 2023-02-02 redis中的配置以及密碼設(shè)置方式_Redis
- 2022-11-15 Django?使用VScode?創(chuàng)建工程的詳細(xì)步驟_python
- 2022-07-30 python實(shí)現(xiàn)網(wǎng)上購(gòu)物系統(tǒng)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支