当前位置:   article > 正文

android 简单快速 实现滚轮控件WheelView(类似DatePicker/TimePicker)_android 时间滚轮控件

android 时间滚轮控件

github 地址:GitHub - Bigkoo/Android-PickerView: This is a picker view for android , support linkage effect, timepicker and optionspicker.(时间选择器、省市区三级联动)

https://github.com/Bigkoo/Android-PickerView

1.引用库

implementation 'com.contrarywind:Android-PickerView:4.1.9'

2. 实现逻辑

  1. public class TimeSelectDialog {
  2. private Activity activity;
  3. private ViewGroup contentView;
  4. private View view;
  5. private WheelView wvHour;
  6. private WheelView wvMinute;
  7. private List<String> hourList;
  8. private List<String> minuteList;
  9. public TimeSelectDialog(Activity activity) {
  10. this.activity = activity;
  11. contentView = activity.findViewById(android.R.id.content);
  12. view = LayoutInflater.from(activity).inflate(R.layout.dialog_time_select, null);
  13. wvHour = view.findViewById(R.id.wv_hour);
  14. wvMinute = view.findViewById(R.id.wv_minute);
  15. TextView tvCancel = view.findViewById(R.id.tv_cancel);
  16. TextView tvSure = view.findViewById(R.id.tv_sure);
  17. tvCancel.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. dismiss();
  21. }
  22. });
  23. tvSure.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. dismiss();
  27. }
  28. });
  29. //第一种方式
  30. initHourWheelView();
  31. initMinuteWheelView();
  32. //第二种方式
  33. /*initTimePickerBuilder();*/
  34. }
  35. /**
  36. * 初始化时间选择器控件
  37. */
  38. private void initTimePickerBuilder() {
  39. Calendar startDate = Calendar.getInstance();
  40. Calendar endDate = Calendar.getInstance();
  41. //正确设置方式 原因:注意事项有说明
  42. startDate.set(startDate.get(Calendar.YEAR)-50,0,1);//-50往前推50年
  43. endDate.set(endDate.get(Calendar.YEAR)+50,11,31);//+50往后推50年
  44. TimePickerView tpv = new TimePickerBuilder(activity, new OnTimeSelectListener() {
  45. @Override
  46. public void onTimeSelect(Date date, View v) {
  47. }
  48. }).setDecorView(view.findViewById(R.id.ll_tpv_container))//设置父控件
  49. .isDialog(false)//非弹框模式
  50. .setItemVisibleCount(5)//显示5行
  51. .setDate(Calendar.getInstance())//当前时间
  52. .setRangDate(startDate,endDate)//起始终止年月日设定
  53. .build();
  54. //隐藏选择器上面的标题栏
  55. int topbarId = activity.getResources().getIdentifier("rv_topbar", "id", activity.getPackageName());
  56. tpv.findViewById(topbarId).setVisibility(View.GONE);
  57. tpv.show();//显示
  58. }
  59. /**
  60. * 小时
  61. */
  62. private void initHourWheelView() {
  63. hourList = new ArrayList<>();
  64. for (int i = 0; i < 24; i++) {
  65. hourList.add(i+"时");
  66. }
  67. ArrayWheelAdapter hourAdapter = new ArrayWheelAdapter(hourList);
  68. wvHour.setAdapter(hourAdapter);
  69. wvHour.setCyclic(true); //取消循环显示数据
  70. wvHour.setCurrentItem(0); //当前显示第一条
  71. wvHour.setItemsVisibleCount(5); //可见范围为5个
  72. wvHour.setOnItemSelectedListener(new OnItemSelectedListener() {
  73. @SuppressLint("SetTextI18n")
  74. @Override
  75. public void onItemSelected(int index) {
  76. }
  77. });
  78. }
  79. /**
  80. * 分时
  81. */
  82. private void initMinuteWheelView() {
  83. minuteList = new ArrayList<>();
  84. for (int i = 0; i < 60; i++) {
  85. minuteList.add(i+"分");
  86. }
  87. ArrayWheelAdapter minuteAdapter = new ArrayWheelAdapter(minuteList);
  88. wvMinute.setAdapter(minuteAdapter);
  89. wvMinute.setCyclic(true); //取消循环显示数据
  90. wvMinute.setCurrentItem(0); //当前显示第一条
  91. wvMinute.setItemsVisibleCount(5); //可见范围为5个
  92. wvMinute.setOnItemSelectedListener(new OnItemSelectedListener() {
  93. @SuppressLint("SetTextI18n")
  94. @Override
  95. public void onItemSelected(int index) {
  96. }
  97. });
  98. }
  99. /**
  100. * 显示dialog(包含动画)
  101. */
  102. public void show(OnDismissListener onDismissListener) {
  103. this.onDismissListener = onDismissListener;
  104. Animation animation = AnimationUtils.loadAnimation(activity, R.anim.dialog_user_gender_in_anim);
  105. view.setAnimation(animation);
  106. contentView.addView(view);
  107. }
  108. /**
  109. * 移除dialog(包含动画)
  110. */
  111. public void dismiss() {
  112. Animation animation = AnimationUtils.loadAnimation(activity, R.anim.dialog_user_gender_out_anim);
  113. view.setAnimation(animation);
  114. contentView.removeView(view);
  115. if (onDismissListener != null) {
  116. onDismissListener.onDismiss(null);
  117. }
  118. }
  119. private OnDismissListener onDismissListener;
  120. public void setOnDismissListener(OnDismissListener onDismissListener) {
  121. this.onDismissListener = onDismissListener;
  122. }
  123. public interface OnDismissListener {
  124. void onDismiss(String timeStr);
  125. }
  126. }

 2.布局实现 dialog_time_select.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#80000000">
  7. <LinearLayout
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center"
  11. android:orientation="vertical"
  12. android:background="@android:color/white"
  13. android:paddingStart="40dp"
  14. android:paddingTop="20dp"
  15. android:paddingEnd="40dp"
  16. android:paddingBottom="20dp">
  17. <TextView
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_gravity="center_horizontal"
  21. android:layout_marginBottom="30dp"
  22. android:text="自定义时间"
  23. android:textColor="@android:color/black"
  24. android:textSize="24sp"
  25. android:textStyle="bold"
  26. app:layout_constraintEnd_toEndOf="parent"
  27. app:layout_constraintStart_toStartOf="parent"
  28. app:layout_constraintTop_toTopOf="parent" />
  29. <!-- 时间选择器父控件 -->
  30. <LinearLayout
  31. android:id="@+id/ll_tpv_container"
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:layout_centerInParent="true"
  35. android:background="@android:color/white"
  36. android:orientation="horizontal"
  37. android:paddingTop="30dp">
  38. <com.contrarywind.view.WheelView
  39. android:id="@+id/wv_hour"
  40. android:layout_width="0dp"
  41. android:layout_height="match_parent"
  42. android:layout_weight="1" />
  43. <com.contrarywind.view.WheelView
  44. android:id="@+id/wv_minute"
  45. android:layout_width="0dp"
  46. android:layout_height="match_parent"
  47. android:layout_weight="1" />
  48. </LinearLayout>
  49. <LinearLayout
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:layout_gravity="center"
  53. android:layout_marginTop="20dp"
  54. android:orientation="horizontal">
  55. <TextView
  56. android:id="@+id/tv_cancel"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_gravity="center_horizontal"
  60. android:gravity="center"
  61. android:paddingStart="60dp"
  62. android:paddingTop="15dp"
  63. android:paddingEnd="60dp"
  64. android:paddingBottom="15dp"
  65. android:background="@android:color/darker_gray"
  66. android:text="关闭"
  67. android:textColor="#ffb300"
  68. android:textSize="18sp" />
  69. <TextView
  70. android:id="@+id/tv_sure"
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:gravity="center"
  74. android:paddingStart="60dp"
  75. android:paddingTop="15dp"
  76. android:paddingEnd="60dp"
  77. android:paddingBottom="15dp"
  78. android:layout_marginStart="20dp"
  79. android:text="确定"
  80. android:background="#ffb300"
  81. android:textColor="@android:color/white"
  82. android:textSize="18sp" />
  83. </LinearLayout>
  84. </LinearLayout>
  85. </FrameLayout>

3.dialog动画

dialog_in_anim.xml

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

dialog_out_anim.xml

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

4.时间选择器弹框:

new TimeSelectDialog(activity).show(null);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/273578
推荐阅读
相关标签
  

闽ICP备14008679号