当前位置:   article > 正文

Android滚轮时间选择控件(可扩展自定义)_android 时间滑动控件

android 时间滑动控件

项目需求中有个功能模块需要用到时间选择控件,但是android系统自带的太丑了,只能自己优化下,结合WheelView实现滚轮选择日期,好像网上也挺多这种文章的。但是适用范围还是不同,希望这个能够对需求相同的朋友有一定帮助。控件标题还有年月日时分秒这些可以自己控制是否显示,先来看效果。

1.有年月日时分的开始时间


2.只有年月日的结束时间


3.用于有时身份证到期的时间选择(分为勾选长期和直接选择时间两种,另外长期后面自己也可以进行扩展)


4.项目结构


5.直接贴代码,代码里面注释很详细

  1. <span style="font-size:18px;"><span style="font-size:14px;">package com.andrew.datechoosewheelviewdemo;
  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.os.Looper;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.LinearLayout;
  11. import android.widget.TextView;
  12. import com.andrew.datechoosewheelviewdemo.widget.OnWheelChangedListener;
  13. import com.andrew.datechoosewheelviewdemo.widget.OnWheelScrollListener;
  14. import com.andrew.datechoosewheelviewdemo.widget.WheelView;
  15. import com.andrew.datechoosewheelviewdemo.widget.adapters.AbstractWheelTextAdapter;
  16. import java.util.ArrayList;
  17. import java.util.Calendar;
  18. /**
  19. * 使用说明:1.showLongTerm()是否显示长期选项
  20. * 2.setTimePickerGone隐藏时间选择
  21. * 3.接口DateChooseInterface
  22. *
  23. * 用于时间日期的选择
  24. * Created by liuhongxia on 2016/4/16.
  25. */
  26. public class DateChooseWheelViewDialog extends Dialog implements View.OnClickListener {
  27. //控件
  28. private WheelView mYearWheelView;
  29. private WheelView mDateWheelView;
  30. private WheelView mHourWheelView;
  31. private WheelView mMinuteWheelView;
  32. private CalendarTextAdapter mDateAdapter;
  33. private CalendarTextAdapter mHourAdapter;
  34. private CalendarTextAdapter mMinuteAdapter;
  35. private CalendarTextAdapter mYearAdapter;
  36. private TextView mTitleTextView;
  37. private Button mSureButton;
  38. private Dialog mDialog;
  39. private Button mCloseDialog;
  40. private LinearLayout mLongTermLayout;
  41. private TextView mLongTermTextView;
  42. //变量
  43. private ArrayList<String> arry_date = new ArrayList<String>();
  44. private ArrayList<String> arry_hour = new ArrayList<String>();
  45. private ArrayList<String> arry_minute = new ArrayList<String>();
  46. private ArrayList<String> arry_year = new ArrayList<String>();
  47. private int nowDateId = 0;
  48. private int nowHourId = 0;
  49. private int nowMinuteId = 0;
  50. private int nowYearId = 0;
  51. private String mYearStr;
  52. private String mDateStr;
  53. private String mHourStr;
  54. private String mMinuteStr;
  55. private boolean mBlnBeLongTerm = false;//是否需要长期
  56. private boolean mBlnTimePickerGone = false;//时间选择是否显示
  57. //常量
  58. private final int MAX_TEXT_SIZE = 18;
  59. private final int MIN_TEXT_SIZE = 16;
  60. private Context mContext;
  61. private DateChooseInterface dateChooseInterface;
  62. public DateChooseWheelViewDialog(Context context, DateChooseInterface dateChooseInterface) {
  63. super(context);
  64. this.mContext = context;
  65. this.dateChooseInterface = dateChooseInterface;
  66. mDialog = new Dialog(context,R.style.dialog);
  67. initView();
  68. initData();
  69. }
  70. private void initData() {
  71. initYear();
  72. initDate();
  73. initHour();
  74. initMinute();
  75. initListener();
  76. }
  77. /**
  78. * 初始化滚动监听事件
  79. */
  80. private void initListener() {
  81. //年份*****************************
  82. mYearWheelView.addChangingListener(new OnWheelChangedListener() {
  83. @Override
  84. public void onChanged(WheelView wheel, int oldValue, int newValue) {
  85. String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
  86. setTextViewStyle(currentText, mYearAdapter);
  87. mYearStr = arry_year.get(wheel.getCurrentItem()) + "";
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/278839
推荐阅读
相关标签
  

闽ICP备14008679号