当前位置:   article > 正文

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

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

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. initHourWheelView();
  30. initMinuteWheelView();
  31. }
  32. /**
  33. * 小时
  34. */
  35. private void initHourWheelView() {
  36. hourList = new ArrayList<>();
  37. for (int i = 0; i < 24; i++) {
  38. hourList.add(i+"时");
  39. }
  40. ArrayWheelAdapter hourAdapter = new ArrayWheelAdapter(hourList);
  41. wvHour.setAdapter(hourAdapter);
  42. wvHour.setCyclic(true); //取消循环显示数据
  43. wvHour.setCurrentItem(0); //当前显示第一条
  44. wvHour.setItemsVisibleCount(5); //可见范围为5个
  45. wvHour.setOnItemSelectedListener(new OnItemSelectedListener() {
  46. @SuppressLint("SetTextI18n")
  47. @Override
  48. public void onItemSelected(int index) {
  49. }
  50. });
  51. }
  52. /**
  53. * 分时
  54. */
  55. private void initMinuteWheelView() {
  56. minuteList = new ArrayList<>();
  57. for (int i = 0; i < 60; i++) {
  58. minuteList.add(i+"分");
  59. }
  60. ArrayWheelAdapter minuteAdapter = new ArrayWheelAdapter(minuteList);
  61. wvMinute.setAdapter(minuteAdapter);
  62. wvMinute.setCyclic(true); //取消循环显示数据
  63. wvMinute.setCurrentItem(0); //当前显示第一条
  64. wvMinute.setItemsVisibleCount(5); //可见范围为5个
  65. wvMinute.setOnItemSelectedListener(new OnItemSelectedListener() {
  66. @SuppressLint("SetTextI18n")
  67. @Override
  68. public void onItemSelected(int index) {
  69. }
  70. });
  71. }
  72. /**
  73. * 显示dialog(包含动画)
  74. */
  75. public void show(OnDismissListener onDismissListener) {
  76. this.onDismissListener = onDismissListener;
  77. Animation animation = AnimationUtils.loadAnimation(activity, R.anim.dialog_user_gender_in_anim);
  78. view.setAnimation(animation);
  79. contentView.addView(view);
  80. }
  81. /**
  82. * 移除dialog(包含动画)
  83. */
  84. public void dismiss() {
  85. Animation animation = AnimationUtils.loadAnimation(activity, R.anim.dialog_user_gender_out_anim);
  86. view.setAnimation(animation);
  87. contentView.removeView(view);
  88. if (onDismissListener != null) {
  89. onDismissListener.onDismiss(null);
  90. }
  91. }
  92. private OnDismissListener onDismissListener;
  93. public void setOnDismissListener(OnDismissListener onDismissListener) {
  94. this.onDismissListener = onDismissListener;
  95. }
  96. public interface OnDismissListener {
  97. void onDismiss(String timeStr);
  98. }
  99. }

 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. <LinearLayout
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:layout_centerInParent="true"
  33. android:background="@android:color/white"
  34. android:orientation="horizontal"
  35. android:paddingTop="30dp">
  36. <com.contrarywind.view.WheelView
  37. android:id="@+id/wv_hour"
  38. android:layout_width="0dp"
  39. android:layout_height="match_parent"
  40. android:layout_weight="1" />
  41. <com.contrarywind.view.WheelView
  42. android:id="@+id/wv_minute"
  43. android:layout_width="0dp"
  44. android:layout_height="match_parent"
  45. android:layout_weight="1" />
  46. </LinearLayout>
  47. <LinearLayout
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_gravity="center"
  51. android:layout_marginTop="20dp"
  52. android:orientation="horizontal">
  53. <TextView
  54. android:id="@+id/tv_cancel"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_gravity="center_horizontal"
  58. android:gravity="center"
  59. android:paddingStart="60dp"
  60. android:paddingTop="15dp"
  61. android:paddingEnd="60dp"
  62. android:paddingBottom="15dp"
  63. android:background="@android:color/darker_gray"
  64. android:text="关闭"
  65. android:textColor="#ffb300"
  66. android:textSize="18sp" />
  67. <TextView
  68. android:id="@+id/tv_sure"
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:gravity="center"
  72. android:paddingStart="60dp"
  73. android:paddingTop="15dp"
  74. android:paddingEnd="60dp"
  75. android:paddingBottom="15dp"
  76. android:layout_marginStart="20dp"
  77. android:text="确定"
  78. android:background="#ffb300"
  79. android:textColor="@android:color/white"
  80. android:textSize="18sp" />
  81. </LinearLayout>
  82. </LinearLayout>
  83. </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/笔触狂放9/article/detail/247722
推荐阅读
相关标签
  

闽ICP备14008679号