Android日期时间选择器

简介:

Android 日期时间选择器

日期选择器是很多应用所具备的,比如设置一些任务的开始和结束时间。为了方便用户的同时也为了界面的好看,很多都是采用日期选择器,我在网上看了一下。很多的日期选择器个人感觉不是很好看,但是修改起来也有点麻烦,于是自己就写了一个demo。至于界面效果个人感觉也是很low,毕竟鄙人不是搞UI的,所以也就凑合着看吧。这些都不重要,因为这些是可以修改的。

如果想实现具有年月日的请看下面的注意里面的内容,下图是实现的分钟为00 15 30 45的如果想要0-59的请看下面的注意里面的内容

如果需要的是仿iOS的带有星期几的

实现的分钟为00 15 30 45的如果想要0-59

首先界面弹出PopupWindow动画的实现,具体的代码如下

进入动画

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" > 
  3.  
  4.     <translate 
  5.         android:duration="500" 
  6.         android:fromYDelta="100.0%p" 
  7.         android:toYDelta="45" /> 
  8.  
  9. </set 

退出动画

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" > 
  3.  
  4.     <translate 
  5.         android:duration="500" 
  6.         android:fromYDelta="0.0" 
  7.         android:toYDelta="100.0%p" /> 
  8.  
  9. </set 

主要界面的内容

 
 
  1. public class MainActivity extends Activity implements View.OnClickListener{ 
  2.     private TextView tv_house_time; 
  3.     private TextView tv_center; 
  4.     private WheelMain wheelMainDate; 
  5.     private String beginTime; 
  6.  
  7.     @Override 
  8.     protected void onCreate(Bundle savedInstanceState) { 
  9.         // TODO Auto-generated method stub 
  10.         super.onCreate(savedInstanceState); 
  11.         setContentView(R.layout.activity_main); 
  12.         initView(); 
  13.         initEvent(); 
  14.     } 
  15.  
  16.     private void initEvent() { 
  17.         tv_house_time.setOnClickListener(this); 
  18.     } 
  19.  
  20.     private void initView() { 
  21.         tv_house_time = (TextView) findViewById(R.id.tv_house_time); 
  22.         tv_center = (TextView) findViewById(R.id.tv_center); 
  23.     } 
  24.  
  25.     private java.text.DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
  26.     public void showBottoPopupWindow() { 
  27.         WindowManager manager = (WindowManager)getSystemService(Context.WINDOW_SERVICE); 
  28.         Display defaultDisplay = manager.getDefaultDisplay(); 
  29.         DisplayMetrics outMetrics = new DisplayMetrics(); 
  30.         defaultDisplay.getMetrics(outMetrics); 
  31.         int width = outMetrics.widthPixels; 
  32.                 View menuView = LayoutInflater.from(this).inflate(R.layout.show_popup_window,null); 
  33.         final PopupWindow mPopupWindow = new PopupWindow(menuView, (int)(width*0.8), 
  34.                 ActionBar.LayoutParams.WRAP_CONTENT); 
  35.         ScreenInfo screenInfoDate = new ScreenInfo(this); 
  36.         wheelMainDate = new WheelMain(menuView, true); 
  37.         wheelMainDate.screenheight = screenInfoDate.getHeight(); 
  38.         String time = DateUtils.currentMonth().toString(); 
  39.         Calendar calendar = Calendar.getInstance(); 
  40.         if (JudgeDate.isDate(time"yyyy-MM-DD")) { 
  41.             try { 
  42.                 calendar.setTime(new Date(time)); 
  43.             } catch (Exception e) { 
  44.                 e.printStackTrace(); 
  45.             } 
  46.         } 
  47.         int year = calendar.get(Calendar.YEAR); 
  48.         int month = calendar.get(Calendar.MONTH); 
  49.         int day = calendar.get(Calendar.DAY_OF_MONTH); 
  50.         int hours = calendar.get(Calendar.HOUR_OF_DAY); 
  51.         int minute = calendar.get(Calendar.MINUTE); 
  52.         wheelMainDate.initDateTimePicker(yearmonthday, hours,minute); 
  53.         final String currentTime = wheelMainDate.getTime().toString(); 
  54.         mPopupWindow.setAnimationStyle(R.style.AnimationPreview); 
  55.         mPopupWindow.setTouchable(true); 
  56.         mPopupWindow.setFocusable(true); 
  57.         mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); 
  58.         mPopupWindow.showAtLocation(tv_center, Gravity.CENTER, 0, 0); 
  59.         mPopupWindow.setOnDismissListener(new poponDismissListener()); 
  60.         backgroundAlpha(0.6f); 
  61.         TextView tv_cancle = (TextView) menuView.findViewById(R.id.tv_cancle); 
  62.         TextView tv_ensure = (TextView) menuView.findViewById(R.id.tv_ensure); 
  63.         TextView tv_pop_title = (TextView) menuView.findViewById(R.id.tv_pop_title); 
  64.         tv_pop_title.setText("选择起始时间"); 
  65.         tv_cancle.setOnClickListener(new View.OnClickListener() { 
  66.             @Override 
  67.             public void onClick(View arg0) { 
  68.                 mPopupWindow.dismiss(); 
  69.                 backgroundAlpha(1f); 
  70.             } 
  71.         }); 
  72.         tv_ensure.setOnClickListener(new View.OnClickListener() { 
  73.  
  74.             @Override 
  75.             public void onClick(View arg0) { 
  76.                 beginTime = wheelMainDate.getTime().toString(); 
  77.                 try { 
  78.                     Date begin = dateFormat.parse(currentTime); 
  79.                     Date end = dateFormat.parse(beginTime); 
  80.                     tv_house_time.setText(DateUtils.currentTimeDeatil(begin)); 
  81.                 } catch (ParseException e) { 
  82.                     e.printStackTrace(); 
  83.                 } 
  84.                 mPopupWindow.dismiss(); 
  85.                 backgroundAlpha(1f); 
  86.             } 
  87.         }); 
  88.     } 
  89.  
  90.     public void backgroundAlpha(float bgAlpha) { 
  91.         WindowManager.LayoutParams lp = getWindow().getAttributes(); 
  92.         lp.alpha = bgAlpha; 
  93.         getWindow().setAttributes(lp); 
  94.     } 
  95.  
  96.     @Override 
  97.     public void onClick(View v) { 
  98.         switch (v.getId()){ 
  99.             case R.id.tv_house_time: 
  100.                 showBottoPopupWindow(); 
  101.                 break; 
  102.         } 
  103.     } 
  104.  
  105.     class poponDismissListener implements PopupWindow.OnDismissListener { 
  106.         @Override 
  107.         public void onDismiss() { 
  108.             backgroundAlpha(1f); 
  109.         } 
  110.  
  111.     } 
  112.  

布局内容的

 
 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:id="@+id/rel_select" 
  3.     android:layout_width="wrap_content" 
  4.     android:layout_height="wrap_content" 
  5.     android:layout_centerInParent="true" 
  6.     android:layout_margin="10dp" 
  7.     android:background="@drawable/border_circle_radius" 
  8.     android:orientation="vertical" > 
  9. <TextView 
  10.     android:background="#2F0F9980" 
  11.     android:padding="10dp" 
  12.     android:id="@+id/tv_pop_title" 
  13.     android:textSize="18sp" 
  14.     android:gravity="center" 
  15.     android:textColor="#301616" 
  16.     android:layout_width="match_parent" 
  17.     android:layout_height="wrap_content" /> 
  18.     <LinearLayout 
  19.         android:id="@+id/timePicker1" 
  20.         android:layout_width="match_parent" 
  21.         android:layout_height="wrap_content" 
  22.         android:orientation="horizontal" > 
  23.         <liuyongxiang.robert.com.testtime.wheelview.WheelView 
  24.             android:id="@+id/year" 
  25.             android:layout_width="wrap_content" 
  26.             android:layout_weight="1" 
  27.             android:layout_height="wrap_content" /> 
  28.         <liuyongxiang.robert.com.testtime.wheelview.DashedLineView 
  29.             android:layout_width="1dp" 
  30.             android:gravity="center_vertical" 
  31.             android:layout_gravity="center" 
  32.             android:background="@drawable/dotted_line" 
  33.             android:layout_height="match_parent" /> 
  34.         <liuyongxiang.robert.com.testtime.wheelview.WheelView 
  35.             android:id="@+id/month" 
  36.             android:layout_width="wrap_content" 
  37.             android:layout_weight="1" 
  38.             android:layout_height="wrap_content" /> 
  39.         <liuyongxiang.robert.com.testtime.wheelview.DashedLineView 
  40.             android:layout_width="1dp" 
  41.             android:gravity="center_vertical" 
  42.             android:layout_gravity="center" 
  43.             android:background="@drawable/dotted_line" 
  44.             android:layout_height="match_parent" /> 
  45.         <liuyongxiang.robert.com.testtime.wheelview.WheelView 
  46.             android:id="@+id/day" 
  47.             android:layout_width="wrap_content" 
  48.             android:layout_weight="1" 
  49.             android:layout_height="wrap_content" /> 
  50.         <liuyongxiang.robert.com.testtime.wheelview.DashedLineView 
  51.             android:layout_width="1dp" 
  52.             android:background="@drawable/dotted_line" 
  53.             android:layout_height="match_parent" /> 
  54.         <liuyongxiang.robert.com.testtime.wheelview.WheelView 
  55.             android:id="@+id/hour" 
  56.             android:layout_width="wrap_content" 
  57.             android:layout_weight="1" 
  58.             android:layout_height="wrap_content" /> 
  59.         <liuyongxiang.robert.com.testtime.wheelview.DashedLineView 
  60.             android:layout_width="1dp" 
  61.             android:background="@drawable/dotted_line" 
  62.             android:layout_height="match_parent" /> 
  63.         <liuyongxiang.robert.com.testtime.wheelview.WheelView 
  64.             android:id="@+id/mins" 
  65.             android:layout_width="wrap_content" 
  66.             android:layout_weight="1" 
  67.             android:layout_height="wrap_content" /> 
  68.     </LinearLayout> 
  69.     <LinearLayout 
  70.         android:layout_width="match_parent" 
  71.         android:layout_height="wrap_content" 
  72.         android:background="#2F0F9980" 
  73.         android:padding="10dp" 
  74.         android:orientation="horizontal" > 
  75.  
  76.         <TextView 
  77.             android:id="@+id/tv_cancle" 
  78.             android:layout_width="0dp" 
  79.             android:layout_height="wrap_content" 
  80.             android:gravity="center" 
  81.             android:paddingLeft="20dp" 
  82.             android:paddingRight="20dp" 
  83.             android:padding="5dp" 
  84.             android:textSize="18sp" 
  85.             android:textColor="#ff0000" 
  86.             android:layout_weight="1" 
  87.             android:background="@drawable/btn_pop" 
  88.             android:text="取消" /> 
  89.  
  90.         <TextView 
  91.             android:layout_width="0dp" 
  92.             android:layout_height="wrap_content" 
  93.             android:layout_weight="3" /> 
  94.  
  95.         <TextView 
  96.             android:id="@+id/tv_ensure" 
  97.             android:layout_width="0dp" 
  98.             android:layout_height="wrap_content" 
  99.             android:gravity="center" 
  100.             android:textColor="#414341" 
  101.             android:padding="5dp" 
  102.             android:paddingLeft="20dp" 
  103.             android:paddingRight="20dp" 
  104.             android:layout_weight="1" 
  105.             android:textSize="18sp" 
  106.             android:background="@drawable/btn_pop" 
  107.             android:text="确定" /> 
  108.     </LinearLayout> 
  109.  
  110. </LinearLayout>  

请注意

MainActivity里面的显示时间的 tv_house_time.setText(DateUtils.currentTimeDeatil(begin));需要更改为

tv_house_time.setText(DateUtils.formateStringH(beginTime,DateUtils.yyyyMMddHHmm));否则现实的额时间为00:00

修改后的

将WheelMain里面的以下代码

 
 
  1. wv_mins.setAdapter(adapter); 
  2. wv_mins.setCyclic(true);// 可循环滚动 
  3. wv_mins.setLabel(“分”);// 添加文字 
  4. int min = setMinute(m); 
  5. wv_mins.setCurrentItem(min);  

更换为

 
 
  1. wv_mins.setAdapter(new NumericWheelAdapter( 
  2. 0, 59)); 
  3. wv_mins.setCyclic(true);// 可循环滚动 
  4. wv_mins.setLabel(“分”);// 添加文字 
  5. wv_mins.setCurrentItem(m);  

还需要将

 
 
  1. int minute = Integer.valueOf(adapter.getItem(wv_mins.getCurrentItem())); 

改为

 
 
  1. int minute = wv_mins.getCurrentItem(); 

会将分钟更改为从0到59

如果不想要时间只想要年月日的话只需要

 
 
  1. if (hasSelectTime) { 
  2.  
  3. wv_hours.setVisibility(View.GONE); 
  4.  
  5. wv_mins.setVisibility(View.GONE); 
  6.  
  7. else { 
  8.  
  9. wv_hours.setVisibility(View.GONE); 
  10.  
  11. wv_mins.setVisibility(View.GONE); 
  12.  
  13. wv_day.setVisibility(View.GONE); 
  14.  
  15.  

将这段代码放开就可以了还要将以下绿色区域内的代码去掉

 

还需要将 MainActivty里的如下代码

 
 
  1. wheelMainDate.initDateTimePicker(yearmonthday, hours,minute); 

更改为

 
 
  1. wheelMainDate.initDateTimePicker(yearmonthday); 

还有 wheelMain里的

 
 
  1. if (!hasSelectTime) { 
  2.         sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-"
  3.                 .append(strMon).append("-"
  4.                 .append(strDay).append("  ").append(strHour).append(":").append(strMin); 
  5.     }else
  6.         sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-"
  7.                 .append(strMon).append("-"
  8.                 .append(strDay).append("  ").append(strHour).append(":").append(strMin); 
  9.     }  

需要修改为

 
 
  1. if (!hasSelectTime) {      
  2.    sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-"
  3.                 .append(strMon).append("-"
  4.                 .append(strDay); 
  5.     }else
  6.         sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-"
  7.                 .append(strMon).append("-"
  8.                 .append(strDay); 
  9.     }  

实现效果如下图

实现效果






本文作者:佚名
来源:51CTO
目录
相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
40 1
|
4月前
|
Android开发
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
34 0
|
21天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
9月前
|
搜索推荐 Java Android开发
如何自定义 Android 日期选择器,实现各种个性化的效果?
如何自定义 Android 日期选择器,实现各种个性化的效果?
173 0
|
Java Android开发
【安卓开发】Android中日期选择器DatePicker和TimePicker的使用
【安卓开发】Android中日期选择器DatePicker和TimePicker的使用
680 0
【安卓开发】Android中日期选择器DatePicker和TimePicker的使用
|
程序员 Android开发 开发者
Android开发:获取当前系统时间和日期的方法
最近接手了公司的一个Android项目,一直在处理Android项目的App的开发,作为半路起家来说,总结了一些Android开发的心得和知识点,然后就写下来记录一下,分享给有需要的开发者查阅交流。那么本篇博文就来分享一下在Android开发过程中,涉及到获取系统当前日期和时间的方法,知识点虽然很常见,但是很实用。
1898 0
Android开发:获取当前系统时间和日期的方法
Java&Android获取当前日期、时间、星期几、获取指定格式的日期时间、时间戳工具类包含使用示例
Java&Android获取当前日期、时间、星期几、获取指定格式的日期时间、时间戳工具类包含使用示例
583 0
Java&Android获取当前日期、时间、星期几、获取指定格式的日期时间、时间戳工具类包含使用示例
|
XML JSON Java
android 继承popupWindow实现时间、地址选择器
日期选择、地址选择,都是开发中少不了的功能,下面通过自定义的形式,同一套代码实现时间选择与地址选择,通过构造方法的不同来实现。
443 0
android 继承popupWindow实现时间、地址选择器
|
Android开发
Android 日期选择器之DatePickerDialog
Android 日期选择器之DatePickerDialog
255 0
Android 日期选择器之DatePickerDialog
|
Android开发
Android日期选择器,年月日判断处理。
Android日期选择器,年月日判断处理。
266 0
Android日期选择器,年月日判断处理。