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. 实现逻辑
public class TimeSelectDialog { private Activity activity; private ViewGroup contentView; private View view; private WheelView wvHour; private WheelView wvMinute; private ListhourList; private List minuteList; public TimeSelectDialog(Activity activity) { this.activity = activity; contentView = activity.findViewById(android.R.id.content); view = LayoutInflater.from(activity).inflate(R.layout.dialog_time_select, null); wvHour = view.findViewById(R.id.wv_hour); wvMinute = view.findViewById(R.id.wv_minute); TextView tvCancel = view.findViewById(R.id.tv_cancel); TextView tvSure = view.findViewById(R.id.tv_sure); tvCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); tvSure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); //第一种方式 initHourWheelView(); initMinuteWheelView(); //第二种方式 /*initTimePickerBuilder();*/ } /** * 初始化时间选择器控件 */ private void initTimePickerBuilder() { Calendar startDate = Calendar.getInstance(); Calendar endDate = Calendar.getInstance(); //正确设置方式 原因:注意事项有说明 startDate.set(startDate.get(Calendar.YEAR)-50,0,1);//-50往前推50年 endDate.set(endDate.get(Calendar.YEAR)+50,11,31);//+50往后推50年 TimePickerView tpv = new TimePickerBuilder(activity, new OnTimeSelectListener() { @Override public void onTimeSelect(Date date, View v) { } }).setDecorView(view.findViewById(R.id.ll_tpv_container))//设置父控件 .isDialog(false)//非弹框模式 .setItemVisibleCount(5)//显示5行 .setDate(Calendar.getInstance())//当前时间 .setRangDate(startDate,endDate)//起始终止年月日设定 .build(); //隐藏选择器上面的标题栏 int topbarId = activity.getResources().getIdentifier("rv_topbar", "id", activity.getPackageName()); tpv.findViewById(topbarId).setVisibility(View.GONE); tpv.show();//显示 } /** * 小时 */ private void initHourWheelView() { hourList = new ArrayList<>(); for (int i = 0; i < 24; i++) { hourList.add(i+"时"); } ArrayWheelAdapter hourAdapter = new ArrayWheelAdapter(hourList); wvHour.setAdapter(hourAdapter); wvHour.setCyclic(true); //取消循环显示数据 wvHour.setCurrentItem(0); //当前显示第一条 wvHour.setItemsVisibleCount(5); //可见范围为5个 wvHour.setOnItemSelectedListener(new OnItemSelectedListener() { @SuppressLint("SetTextI18n") @Override public void onItemSelected(int index) { } }); } /** * 分时 */ private void initMinuteWheelView() { minuteList = new ArrayList<>(); for (int i = 0; i < 60; i++) { minuteList.add(i+"分"); } ArrayWheelAdapter minuteAdapter = new ArrayWheelAdapter(minuteList); wvMinute.setAdapter(minuteAdapter); wvMinute.setCyclic(true); //取消循环显示数据 wvMinute.setCurrentItem(0); //当前显示第一条 wvMinute.setItemsVisibleCount(5); //可见范围为5个 wvMinute.setOnItemSelectedListener(new OnItemSelectedListener() { @SuppressLint("SetTextI18n") @Override public void onItemSelected(int index) { } }); } /** * 显示dialog(包含动画) */ public void show(OnDismissListener onDismissListener) { this.onDismissListener = onDismissListener; Animation animation = AnimationUtils.loadAnimation(activity, R.anim.dialog_user_gender_in_anim); view.setAnimation(animation); contentView.addView(view); } /** * 移除dialog(包含动画) */ public void dismiss() { Animation animation = AnimationUtils.loadAnimation(activity, R.anim.dialog_user_gender_out_anim); view.setAnimation(animation); contentView.removeView(view); if (onDismissListener != null) { onDismissListener.onDismiss(null); } } private OnDismissListener onDismissListener; public void setOnDismissListener(OnDismissListener onDismissListener) { this.onDismissListener = onDismissListener; } public interface OnDismissListener { void onDismiss(String timeStr); } }
2.布局实现 dialog_time_select.xml
3.dialog动画
dialog_in_anim.xml
dialog_out_anim.xml
4.时间选择器弹框:
new TimeSelectDialog(activity).show(null);
还没有评论,来说两句吧...