安卓选择器

devtools/2024/10/24 2:26:16/

一、首先引入依赖库

//时间选择器implementation 'io.github.ShawnLin013:number-picker:2.4.13'

二、自定义时间选择器

public class TimePickerCustom {private final BottomSheetDialog bottomDialog;private final NumberPicker year;private final NumberPicker month;private final NumberPicker day;/*** 时间选择回调**/public interface TimePickerCallback {/*** 回调** @param year 年* @param month 月* @param day 日*/void setDate(int year, int month, int day);}public TimePickerCustom(Context context, String title, TimePickerCallback callback) {// 设置时间选择器的布局以及弹窗的高度bottomDialog = getBottomDialog(context, R.layout.view_time_picker, dpToPx(context, 350));Calendar calendar = Calendar.getInstance();// 设置标题((TextView)bottomDialog.findViewById(R.id.title)).setText(title);// 年year = (NumberPicker)bottomDialog.findViewById(R.id.year);int yearNow = calendar.get(Calendar.YEAR);year.setMinValue(yearNow - 100);year.setMaxValue(yearNow + 100);year.setValue(yearNow);// 月month = (NumberPicker)bottomDialog.findViewById(R.id.month);String[] monthNum = new String[12];for (int i = 0; i < 12; i++) {monthNum[i] = (i + 1) + "月";}month.setMinValue(1);month.setMaxValue(monthNum.length);month.setDisplayedValues(monthNum);month.setValue(calendar.get(Calendar.MONTH));// 日day = (NumberPicker)bottomDialog.findViewById(R.id.day);day.setMinValue(1);int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);String[] newDays = getNewDays(days);day.setMaxValue(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));day.setDisplayedValues(newDays);day.setValue(calendar.get(Calendar.DATE));// 年份和月份更改时对应的天数需要更改year.setOnValueChangedListener((picker, oldVal, newVal) -> {updateNumberOfDays();day.setValue(calendar.get(Calendar.DATE));});month.setOnValueChangedListener((picker, oldVal, newVal) -> {updateNumberOfDays();day.setValue(calendar.get(Calendar.DATE));});// 取消按钮和确定按钮事件绑定View cancel = bottomDialog.findViewById(R.id.cancel);View ok = bottomDialog.findViewById(R.id.ok);if (cancel != null) {cancel.setOnClickListener(v -> bottomDialog.dismiss());}if (ok != null) {ok.setOnClickListener(v -> {bottomDialog.dismiss();callback.setDate(year.getValue(), month.getValue(), day.getValue());});}}/*** 底部弹出框** @param id 弹窗中的布局* @param height 弹窗高度*/private BottomSheetDialog getBottomDialog(Context context, Integer id, int height) {BottomSheetDialog bottomSheet = new BottomSheetDialog(context);// 设置对框框中的布局bottomSheet.setContentView(id);// 设置点击外部是否可以取消bottomSheet.setCancelable(true);FrameLayout bottom = (FrameLayout)bottomSheet.findViewById(com.google.android.material.R.id.design_bottom_sheet);if (bottom != null) {// 设置背景透明颜色bottom.setBackgroundResource(R.color.transparent);// 修改弹窗的高度ViewGroup.LayoutParams layoutParams = bottom.getLayoutParams();layoutParams.height = height;bottom.setLayoutParams(layoutParams);}return bottomSheet;}/*** dp转px*/private int dpToPx(Context context, float dp) {return (int)(dp * context.getResources().getDisplayMetrics().density + 0.5);}/*** 显示*/public void show() {bottomDialog.show();}/*** 设置选中年份** @param yearValue 年*/public void setYearValue(int yearValue) {year.setValue(yearValue);updateNumberOfDays();}/*** 设置选中月份** @param monthValue 月*/public void setMonthValue(int monthValue) {month.setValue(monthValue);updateNumberOfDays();}/*** 设置选中天数** @param dayValue 天*/public void setDayValue(int dayValue) {day.setValue(dayValue);}/*** 更新天数**/private void updateNumberOfDays() {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.YEAR, year.getValue());calendar.set(Calendar.MONTH, (month.getValue() - 1));calendar.set(Calendar.DATE, 1);calendar.roll(Calendar.DATE, -1);int date = calendar.get(Calendar.DATE);day.setMaxValue(date);day.setDisplayedValues(getNewDays(date));}/*** 格式化天数** @param days 天数* @return {@link String[]}*/private String[] getNewDays(int days) {List<String> dayList = new ArrayList<>();for (int i = 0; i < days; i++) {dayList.add((i + 1) + "日");}return dayList.toArray(new String[dayList.size()]);}}

对应view_time_picker.xml布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:background="@drawable/bg_bottom_dialog"><TextViewandroid:id="@+id/cancel"android:layout_width="wrap_content"android:layout_height="40dp"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintBottom_toTopOf="@id/year"android:gravity="center"android:text="取消"android:textColor="#666666"android:clickable="true"android:focusable="true" /><TextViewandroid:id="@+id/title"android:layout_width="0dp"android:layout_height="40dp"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toRightOf="@id/cancel"app:layout_constraintBottom_toTopOf="@id/year"app:layout_constraintRight_toLeftOf="@id/ok"android:gravity="center"android:text="出生日期"android:textColor="#333333"android:textStyle="bold" /><TextViewandroid:id="@+id/ok"android:layout_width="wrap_content"android:layout_height="40dp"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toTopOf="@id/year"android:gravity="center"android:text="确定"android:textColor="#1580C8"android:textStyle="bold"android:clickable="true"android:focusable="true" /><com.shawnlin.numberpicker.NumberPickerandroid:id="@+id/year"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintTop_toBottomOf="@id/title"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@id/month"app:np_formatter="%d"app:np_dividerColor="#2B000000"app:np_dividerThickness="1px"app:np_selectedTextColor="#333333"app:np_selectedTextSize="18sp"app:np_textSize="14sp"app:np_wheelItemCount="5"app:np_wrapSelectorWheel="false" /><com.shawnlin.numberpicker.NumberPickerandroid:id="@+id/month"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintTop_toBottomOf="@id/title"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toRightOf="@id/year"app:layout_constraintRight_toLeftOf="@id/day"app:np_dividerColor="#2B000000"app:np_dividerThickness="1px"app:np_selectedTextColor="#333333"app:np_selectedTextSize="18sp"app:np_textSize="14sp"app:np_wheelItemCount="5"app:np_wrapSelectorWheel="false" /><com.shawnlin.numberpicker.NumberPickerandroid:id="@+id/day"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintTop_toBottomOf="@id/title"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toRightOf="@id/month"app:layout_constraintRight_toRightOf="parent"app:np_dividerColor="#2B000000"app:np_dividerThickness="1px"app:np_selectedTextColor="#333333"app:np_selectedTextSize="18sp"app:np_textSize="14sp"app:np_wheelItemCount="5"app:np_wrapSelectorWheel="false" /></androidx.constraintlayout.widget.ConstraintLayout>

用法

在Mactivity或者Fragment直接调用

TimePickerCustom timePickerCustom = new TimePickerCustom(PersonalInfoActivity.this, "时间选择", (year, month, day) -> {Log.i("timePickerCustom", year + "------" + month + "--------" + day);Message msg = new Message();msg.what = 2;msg.obj = year+"/" + month + "/" + day;handler.sendMessage(msg);});timePickerCustom.show();


http://www.ppmy.cn/devtools/8954.html

相关文章

恒创科技:网站更改域名对seo的影响大吗?

众所周知&#xff0c;搜索引擎优化 (SEO) 对于增加网站的自然流量、提高转化率以及提高品牌知名度至关重要。所以&#xff0c;网站在进行更改域名时&#xff0c;我们应当关注其对 SEO 的影响。 网站更改域名对 SEO 影响大吗? 更改域名会更改网站上每个页面的 URL &#xff0c;…

论文浅尝 | 基于事件溯因学习的复杂时间敏感问答

笔记整理&#xff1a;武绍娟&#xff0c;天津大学博士&#xff0c;研究方向为自然语言处理 发表会议&#xff1a;CoLing 2024 1、动机 时间敏感问答是根据给定长文档回答满足特定时间戳约束的问题&#xff0c;被广泛应用于医疗、金融等领域事件问答。现有工作根据问题在给定文档…

Golang:GORM使用First查询日志中出现大量record not found

感觉是作者故意留下的坑吧&#xff0c;用了很多编程语言的orm实现&#xff0c;第一次发现这个问题&#xff0c;好在作者已经解决了 通过配置日志参数IgnoreRecordNotFoundErrortrue实现忽略数据不存在的日志 完整配置示例 newLogger : logger.New(log.New(os.Stdout, "…

设计模式-工厂模式

工厂模式 1、简单工厂 简单工厂比较简单,它的作用就是把对象的创建放到一个工厂类中,通过参数来创建不同的对象。 在分布式事务框架Seata中,如果发生异常,则需要进行二阶段回滚。 它的过程是,通过事务id找到undoLog记录,然后解析里面的数据生成SQL,将一阶段执行的S…

node端导出excel-用请求排队来限流

需求 有一个会执行luckySheet脚本并且导出excel的node接口&#xff0c;会在每天凌晨执行&#xff0c;但是文件过大时会内存溢出 之前有用worker来实现多线程&#xff08;主要是避免变量污染&#xff09;&#xff0c;但这样只能保证主线程不卡死&#xff0c;几个子线程合起来占用…

20240420

docker 使用不添加sudo前缀 确认group&#xff1a; getent group如果没有docker组&#xff1a;sudo groupadd docker添加用户到docker组&#xff1a; sudo usermod -aG docker freja重启docker&#xff1a; sudo systemctl restart docker测试后依旧报错&#xff0c;如果是还是…

PHP反序列化漏洞原理(附带pikachu靶场演示)

1.反序列化概念 序列化:是将变量转换为可保存或传输的字符串的过程;实现函数是serialize()反序列化:就是在适当的时候把这个字符串再转化成原来的变量使用&#xff0c;就是序列化的逆过程。实现函数是unserialize() 直白一点就是&#xff1a;序列化是把对象转换成字节流&#…

【码农圈子】想加免费的程序员微信群的看过来

群名&#xff1a;码农圈子 很多人后台反应&#xff0c;最近有没有免费的微信技术交流社群 。今天特意写一篇文章来创建一些只有程序猿的微信群。&#xff08;广告党慎入&#xff01;&#xff09; 这些微信技术群都是完全免费&#xff0c;后续也不会收取任何费用 。 群规则 …