Android 日期时间选择器
来源:互联网 发布:mac如何使窗口半屏幕 编辑:程序博客网 时间:2024/05/18 06:28
Android 日期时间选择器
日期选择器是很多应用所具备的,比如设置一些任务的开始和结束时间。为了方便用户的同时也为了界面的好看,很多都是采用日期选择器,我在网上看了一下。很多的日期选择器个人感觉不是很好看,但是修改起来也有点麻烦,于是自己就写了一个demo。至于界面效果个人感觉也是很low,毕竟鄙人不是搞UI的,所以也就凑合着看吧。这些都不重要,因为这些是可以修改的。
如果想实现具有年月日的请看下面的注意里面的内容,下图是实现的分钟为00 15 30 45的如果想要0-59的请看下面的注意里面的内容
如果需要的是仿iOS的带有星期几的
点击此处查看带有星期几的博客
首先界面弹出PopupWindow动画的实现,具体的代码如下
进入动画
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromYDelta="100.0%p" android:toYDelta="45" /></set>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
退出动画
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromYDelta="0.0" android:toYDelta="100.0%p" /></set>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
主要界面的内容
public class MainActivity extends Activity implements View.OnClickListener{ private TextView tv_house_time; private TextView tv_center; private WheelMain wheelMainDate; private String beginTime; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initEvent(); } private void initEvent() { tv_house_time.setOnClickListener(this); } private void initView() { tv_house_time = (TextView) findViewById(R.id.tv_house_time); tv_center = (TextView) findViewById(R.id.tv_center); } private java.text.DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); public void showBottoPopupWindow() { WindowManager manager = (WindowManager)getSystemService(Context.WINDOW_SERVICE); Display defaultDisplay = manager.getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics(); defaultDisplay.getMetrics(outMetrics); int width = outMetrics.widthPixels; View menuView = LayoutInflater.from(this).inflate(R.layout.show_popup_window,null); final PopupWindow mPopupWindow = new PopupWindow(menuView, (int)(width*0.8), ActionBar.LayoutParams.WRAP_CONTENT); ScreenInfo screenInfoDate = new ScreenInfo(this); wheelMainDate = new WheelMain(menuView, true); wheelMainDate.screenheight = screenInfoDate.getHeight(); String time = DateUtils.currentMonth().toString(); Calendar calendar = Calendar.getInstance(); if (JudgeDate.isDate(time, "yyyy-MM-DD")) { try { calendar.setTime(new Date(time)); } catch (Exception e) { e.printStackTrace(); } } int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int hours = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); wheelMainDate.initDateTimePicker(year, month, day, hours,minute); final String currentTime = wheelMainDate.getTime().toString(); mPopupWindow.setAnimationStyle(R.style.AnimationPreview); mPopupWindow.setTouchable(true); mPopupWindow.setFocusable(true); mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); mPopupWindow.showAtLocation(tv_center, Gravity.CENTER, 0, 0); mPopupWindow.setOnDismissListener(new poponDismissListener()); backgroundAlpha(0.6f); TextView tv_cancle = (TextView) menuView.findViewById(R.id.tv_cancle); TextView tv_ensure = (TextView) menuView.findViewById(R.id.tv_ensure); TextView tv_pop_title = (TextView) menuView.findViewById(R.id.tv_pop_title); tv_pop_title.setText("选择起始时间"); tv_cancle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { mPopupWindow.dismiss(); backgroundAlpha(1f); } }); tv_ensure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { beginTime = wheelMainDate.getTime().toString(); try { Date begin = dateFormat.parse(currentTime); Date end = dateFormat.parse(beginTime); tv_house_time.setText(DateUtils.currentTimeDeatil(begin)); } catch (ParseException e) { e.printStackTrace(); } mPopupWindow.dismiss(); backgroundAlpha(1f); } }); } public void backgroundAlpha(float bgAlpha) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = bgAlpha; getWindow().setAttributes(lp); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv_house_time: showBottoPopupWindow(); break; } } class poponDismissListener implements PopupWindow.OnDismissListener { @Override public void onDismiss() { backgroundAlpha(1f); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
布局内容的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rel_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="10dp" android:background="@drawable/border_circle_radius" android:orientation="vertical" ><TextView android:background="#2F0F9980" android:padding="10dp" android:id="@+id/tv_pop_title" android:textSize="18sp" android:gravity="center" android:textColor="#301616" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/timePicker1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <liuyongxiang.robert.com.testtime.wheelview.WheelView android:id="@+id/year" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" /> <liuyongxiang.robert.com.testtime.wheelview.DashedLineView android:layout_width="1dp" android:gravity="center_vertical" android:layout_gravity="center" android:background="@drawable/dotted_line" android:layout_height="match_parent" /> <liuyongxiang.robert.com.testtime.wheelview.WheelView android:id="@+id/month" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" /> <liuyongxiang.robert.com.testtime.wheelview.DashedLineView android:layout_width="1dp" android:gravity="center_vertical" android:layout_gravity="center" android:background="@drawable/dotted_line" android:layout_height="match_parent" /> <liuyongxiang.robert.com.testtime.wheelview.WheelView android:id="@+id/day" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" /> <liuyongxiang.robert.com.testtime.wheelview.DashedLineView android:layout_width="1dp" android:background="@drawable/dotted_line" android:layout_height="match_parent" /> <liuyongxiang.robert.com.testtime.wheelview.WheelView android:id="@+id/hour" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" /> <liuyongxiang.robert.com.testtime.wheelview.DashedLineView android:layout_width="1dp" android:background="@drawable/dotted_line" android:layout_height="match_parent" /> <liuyongxiang.robert.com.testtime.wheelview.WheelView android:id="@+id/mins" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#2F0F9980" android:padding="10dp" android:orientation="horizontal" > <TextView android:id="@+id/tv_cancle" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:paddingLeft="20dp" android:paddingRight="20dp" android:padding="5dp" android:textSize="18sp" android:textColor="#ff0000" android:layout_weight="1" android:background="@drawable/btn_pop" android:text="取消" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" /> <TextView android:id="@+id/tv_ensure" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:textColor="#414341" android:padding="5dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_weight="1" android:textSize="18sp" android:background="@drawable/btn_pop" android:text="确定" /> </LinearLayout></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
请注意
MainActivity里面的显示时间的 tv_house_time.setText(DateUtils.currentTimeDeatil(begin));需要更改为
tv_house_time.setText(DateUtils.formateStringH(beginTime,DateUtils.yyyyMMddHHmm));否则现实的额时间为00:00
修改后的
将WheelMain里面的以下代码
wv_mins.setAdapter(adapter);
wv_mins.setCyclic(true);// 可循环滚动
wv_mins.setLabel(“分”);// 添加文字
int min = setMinute(m);
wv_mins.setCurrentItem(min);
更换为
wv_mins.setAdapter(new NumericWheelAdapter(
0, 59));
wv_mins.setCyclic(true);// 可循环滚动
wv_mins.setLabel(“分”);// 添加文字
wv_mins.setCurrentItem(m);
还需要将
int minute = Integer.valueOf(adapter.getItem(wv_mins.getCurrentItem()));
改为
int minute = wv_mins.getCurrentItem();
会将分钟更改为从0到59
如果不想要时间只想要年月日的话只需要
if (hasSelectTime) {
wv_hours.setVisibility(View.GONE);
wv_mins.setVisibility(View.GONE);
} else { wv_hours.setVisibility(View.GONE); wv_mins.setVisibility(View.GONE); wv_day.setVisibility(View.GONE); } 将这段代码放开就可以了还要将以下绿色区域内的代码去掉
- 1
- 2
- 3
- 4
- 5
- 6
- 7
还需要将 MainActivty里的如下代码
wheelMainDate.initDateTimePicker(year, month, day, hours,minute);
更改为
wheelMainDate.initDateTimePicker(year, month, day);
还有 wheelMain里的
if (!hasSelectTime) {
sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-") .append(strMon).append("-") .append(strDay).append(" ").append(strHour).append(":").append(strMin); }else{ sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-") .append(strMon).append("-") .append(strDay).append(" ").append(strHour).append(":").append(strMin); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
需要修改为
if (!hasSelectTime) {
sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-") .append(strMon).append("-") .append(strDay); }else{ sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-") .append(strMon).append("-") .append(strDay); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
实现效果如下图
最近更新代码到github上面了
Github下载地址
csdn下载地址
如有疑问请留言或请加Android交流群 470707794或Android技术交流群 50208422(此群已满)
扫码关注公众号
- 本文已收录于以下专栏:
-
-
baidu_26732735
2017-07-14 17:02 39楼 - 你好,那个View在实现的时候中间那个选择栏会出现黑色条,改了很久没改过来,求大神指点
-
-
-
baidu_26732735
2017-07-14 17:00 38楼 - 你好,那个View在实现的时候中间那个选择栏会出现黑色条,改了很久没改过来,求大神指点
-
-
-
baidu_26732735
2017-07-14 16:59 37楼 - 你好,那个View在实现的时候中间那个选择栏会出现黑色条,改了很久没改过来,求大神指点
-
相关文章推荐
-
【android】只有小时和分钟的弹出式时间选择器
一、这是什么鬼在最近的项目中需要用到一个只选择小时和分钟的时间选择器,在网上看了以后发现没有合适的,于是自己参考了一些例子后写了一个,并把它发上来,有需要的可以参考。另外本人也是弱菜,欢迎大家指教。下…- Jason___Lu
- 2015-06-03 00:08
- 3869
-
[android] 解决DatePickerDialog和TimePickerDialog控件取消按钮问题
本文是讲述android 4.0版本以上不能显示DatePickerDialog和TimePickerDialog控件取消键的问题,同时点击界面其他地方也能实现修改日期和时间的问题。主要采用自定义布局…- Eastmount
- 2015-01-07 05:01
- 4321
-
AI时代,机器学习该如何入门?
对于机器学习,很多人的观点是:机器学习技术是今后所有技术人员都绕不过的一个门槛。 那么,普通程序员该学习机器学作为一名对机器学习心有向往的程序员,我该以什么样的姿势开始呢?
-
安卓TimePicker,DatePicker日期选择器隐藏分钟或者小时,年月日?
当时找个很多的代码,都没有找到一个好的方法,这里直接上代码:注意的是,,先把全部的时间显示出来再说:大概写了两个,其他的大同小异:private void hidDay…- qq_34900897
- 2017-04-24 13:32
- 390
-
Android-使用TimerPicker和DatePicker选择时间和日期
Android-使用TimerPicker和DatePicker选择时间和日期在安卓App里面,选择时间日期也是很常见的,比如在QQ里面填写个人信息的时候,会让你选择 你的出生日期,比如: 这…- ACM_TH
- 2016-03-27 15:49
- 4388
-
Android 选择日期时间对话框(可选择开始结束时间,已解决弹出键盘问题)
直接行代码: MainActivity:package wkk.demo6;import android.app.AlertDialog;import android.app.DatePicker…- w18756901575
- 2016-05-19 18:44
- 1984
-
Android自定义DataTimePicker(日期选择器)
Android自定义DataTimePicker(日期选择器) 笔者有一段时间没有发表关于Android的文章了,关于Android自定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中。本篇博客给…- wwj_748
- 2014-08-23 15:39
- 142923
-
Android 好看的自定义滚动式日期选择控件
最近接触了日期选择的功能,那么肯定得需要一个日期选择控件,Android 系统有自带的 DatePicker 控件,但是不说这个控件有多难看吧,现在 Android 手机版本那么多,用户弹出来的控件…- liuwan1992
- 2016-09-29 14:14
- 10439
-
Android中日期时间选择器的使用及常见的问题
最近闲来无事,特地写一篇博客,来介绍Android中日期选择器的使用及常见的问题。Android中的日期选择器DatePicker,能实现日期的选择、编辑和修改,虽是一个很简单的组件,但在实际使…- ch1451082329
- 2015-01-28 13:10
- 3462
-
Android 日期时间选择器
日期选择器是很多应用所具备的,比如设置一些任务的开始和结束时间。为了方便用户的同时也为了界面的好看,很多都是采用日期选择器,我在网上看了一下。很多的日期选择器个人感觉不是很好看,但是修改起来也有点麻烦…- u014452224
- 2016-09-07 17:47
- 18310
-
Android中实现日期时间选择器(DatePicker和TimePicker)
注:5.0以上系统日期和时间界面有改变,建议横向排列,避免显示不完整选择日期时间时困难利用Android应用框架提供的DatePicker(日期选择器)和TimePicker(时间选择器)…- u012437660
- 2016-03-17 10:39
- 229
-
android基础-Toast提示框、日历视图(CalendarView)组件、日期、时间选择器DatePicker和TimerPicker等
1. Toast提示框 // 创建一个Toast提示信息 Toast toast = Toast.makeText(MainActivity.this …- qq_20967339
- 2017-08-16 22:07
- 38
-
Android日期时间选择器实现以及自定义大小
原文地址: http://lovelease.iteye.com/blog/2109729本文主要讲两个内容:1.如何将DatePicker和TimePicker放在一个dialog里面;…- u011819464
- 2014-11-10 20:40
- 1215
- 原创
- 69
- 粉丝
- 199
- 喜欢
- 0
- 码云
- 0
- Android 自定义可以滑动和点击的RangeBar
- Android GreenDao实现CRUD和升级详解
- 设计模式——责任链模式
- 设计模式——状态模式
在线课程
- 【直播】大中型UGC信息网站SEO分享–乔向阳
- 【直播】打通Linux脉络 进程、线程和调度–宋宝华
- android 日期时间选择器
- Android 日期时间选择器
- android 日期时间选择器
- Android 时间日期选择器
- Android 日期时间选择器
- android时间日期选择器,监听
- android日期和时间选择器
- Android 日期和时间选择器
- Android仿iPhone的日期时间选择器
- Android 时间日期选择器的用法
- android日期与时间滑动选择器
- Android之日期时间选择器使用方法
- Android日期/时间选择器(工具类)
- Android时间日期选择器,高仿小米
- Github中的Android时间,日期选择器
- Android中的日期和时间选择器
- Android仿ios时间日期选择器
- wheelView自定义android日期时间选择器
- Excel vba 批量修改指定路径下指定名称对应的值
- 获取一个长文件路径(比如”E:\\test\\class\\php\\gre\\aaa\\abc.html”)中的文件名及其后缀
- python网络编程基础
- JavaScript数组去重
- 信息治理——初识内容管理(二)
- Android 日期时间选择器
- 动态规划
- Cassandra 线上环境配置建议
- 一、docker-platform介绍
- CodeForces125E MST Company(根限度为k的最小生成树+二分)
- 大数据量下高并发同步的讲解(不看,保证你后悔)
- Parts of a list -- 7 kyu
- NOIP 2015提高组 跳石头
- 史上最详细Windows版本搭建安装React Native环境配置
62条评论