Android自定义日期选择器

Android开发:实现开发时间选择器和日期选择器组件 如果你想要更灵活地控制日期选择器和时间选择器的外观和行为,你可以选择自定义控件。以下是一个简单的例子,演示了如何使用自定义布局来创建日期选择器和时间选择器。在res/layout文件夹下创建一个XML布局文件,例如,定义你自己的日期选择器界面。-- 自定义界面元素(例如:滚动条、按钮等) --> -- 自定义界面元素(例如:滚动条、按钮等) --> -- 自定义界面元素(例如:滚动条、按钮等) --> 阅读详情

先上图:


布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:gravity="center"
        android:background="@color/mainColor"
        android:text="选择器 Picker"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="日期设置:"
            android:textColor="#656565"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tvDate"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:onClick="chooseDateDialog"
            android:text="2016-06-15"
            android:textColor="#656565"
            android:textSize="18sp" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="#C9C9C9" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="城市设置:"
            android:textColor="#656565"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tvCity"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:text="广东-深圳-福田"
            android:textColor="#656565"
            android:textSize="18sp" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="#C9C9C9" />

</LinearLayout>
接口类 ChooseDateInterface.java

public interface ChooseDateInterface {
    public void sure(int[] newDateArray);
}
日期选择工具类 ChooseDateUtil.java

/**
 * Developer : xiongwenwei@aliyun.com
 * Create Time :2016-6-16 16:25:36
 * Function:日期选择器
 */
public class ChooseDateUtil implements View.OnClickListener, NumberPicker.OnValueChangeListener {

    Context context;
    AlertDialog dialog;
    ChooseDateInterface dateInterface;
    NumberPicker npYear, npMonth, npDay;
    TextView tvCancel, tvSure;
    int[] newDateArray = new int[3];

    public void createDialog(Context context, String[] oldDateArray, ChooseDateInterface dateInterface) {
        this.context = context;
        this.dateInterface = dateInterface;
        newDateArray[0] = Integer.parseInt(oldDateArray[0]);
        newDateArray[1] = Integer.parseInt(oldDateArray[1]);
        newDateArray[2] = Integer.parseInt(oldDateArray[2]);

        dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_choose_date);
        //初始化控件
        tvCancel = (TextView) window.findViewById(R.id.tvCancel);
        tvSure = (TextView) window.findViewById(R.id.tvSure);
        tvCancel.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        npYear = (NumberPicker) window.findViewById(R.id.npYear);
        npMonth = (NumberPicker) window.findViewById(R.id.npMonth);
        npDay = (NumberPicker) window.findViewById(R.id.npDay);
        //设置选择器最小值、最大值
        npYear.setMinValue(1900);
        npYear.setMaxValue(2100);
        npMonth.setMinValue(1);
        npMonth.setMaxValue(12);
        npDay.setMinValue(1);
        npDay.setMaxValue(31);
        //设置选择器初始值
        npYear.setValue(newDateArray[0]);
        npMonth.setValue(newDateArray[1]);
        npDay.setValue(newDateArray[2]);
        //设置监听
        npYear.setOnValueChangedListener(this);
        npMonth.setOnValueChangedListener(this);
        npDay.setOnValueChangedListener(this);
        //去除分割线
        setNumberPickerDividerColor(npYear);
        setNumberPickerDividerColor(npMonth);
        setNumberPickerDividerColor(npDay);
        //设置字体颜色
        setNumberPickerTextColor(npYear, Color.parseColor("#1BC47A"));
        setNumberPickerTextColor(npMonth, Color.parseColor("#1BC47A"));
        setNumberPickerTextColor(npDay, Color.parseColor("#1BC47A"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvCancel:
                dialog.dismiss();
                break;
            case R.id.tvSure:
                dialog.dismiss();
                dateInterface.sure(newDateArray);
                break;
        }
    }

    //选择器选择值监听
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        switch (picker.getId()) {
            case R.id.npYear:
                newDateArray[0] = newVal;
                npDay.setMaxValue(DateUtil.getNumberOfDays(newDateArray[0], newDateArray[1]));
                break;
            case R.id.npMonth:
                newDateArray[1] = newVal;
                npDay.setMaxValue(DateUtil.getNumberOfDays(newDateArray[0], newDateArray[1]));
                break;
            case R.id.npDay:
                newDateArray[2] = newVal;
                break;
        }
    }

    private void setNumberPickerDividerColor(NumberPicker numberPicker) {
        NumberPicker picker = numberPicker;
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值
                    pf.set(picker, new ColorDrawable(context.getResources().getColor(R.color.touming)));// pf.set(picker, new Div)
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
        boolean result = false;
        final int count = numberPicker.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = numberPicker.getChildAt(i);
            if (child instanceof EditText) {
                try {
                    Field selectorWheelPaintField = numberPicker.getClass()
                            .getDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.setAccessible(true);
                    ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
                    ((EditText) child).setTextColor(color);
                    numberPicker.invalidate();
                    result = true;
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}


日期工具类 DateUtil.java

/**
 * Developer : xiongwenwei@aliyun.com
 * Create Time :2016-6-16 16:26:33
 * Function:日期工具类
 */
public class DateUtil {

    //获取某年某月的总天数
    public static int getNumberOfDays(int year, int month) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if(isRunYear(year)){
                    return 29;
                }else{
                    return 28;
                }
        }
        return 100;
    }

    //判断是否闰年
    private static boolean isRunYear(int year) {
        return (year % 4 != 0) || (year % 100 == 0) && (year % 400 != 0);
    }
}
使用调用:

    //Choose Date 选择日期
    public void chooseDateDialog() {
        final ChooseDateUtil dateUtil = new ChooseDateUtil();
        String[] oldDateArray = tvDate.getText().toString().split("-");
        dateUtil.createDialog(this, oldDateArray, new ChooseDateInterface() {
            @Override
            public void sure(int[] newDateArray) {
                tvDate.setText(newDateArray[0] + "-" + newDateArray[1] + "-" + newDateArray[2]);
            }
        });
    }












Android时间日期选择器 本文详细介绍了Android平台上高度可定制的日期时间选择器组件实现。该组件采用iOS风格的滚轮选择器设计,包含日期和时间选择功能,支持国际化适配和暗色模式。核心模块包括通用数组适配器ArrayWheelAdapter、数字范围适配器NumericWheelAdapter、滚轮视图组件WheelView以及日期时间选择对话框SprDatePicker和SprTimePicker。组件特性包括智能日期联动(自动计算闰年月份天数)、国际化支持(动态调整日期顺序)、平滑滚动体验、多感官反馈(震动+声音)以及暗色模 阅读详情

相关推荐

Android自定义日期选择器组件详解

自定义日期选择器组件为开发者提供了超出原生组件的灵活性和可定制性,使得在不同应用中实现一致且符合品牌调性的用户界面成为可能。本章将深入探讨这一主题,首先从理论基础入手,之后详细讲解实现的关键技术。实现一个自定义日期选择器组件,需要遵循以下基本步骤:创建自定义View类:继承View或者ViewGroup类,并在其中定义日期选择器的逻辑和外观。定义布局属性:在attrs.xml中定义可定制的属性,如颜色、尺寸和日期格式等。绘制组件界面:重写onDraw。

weixin_35751412的博客 1286

Android自定义时间选择器

Android自定义时间选择器

提笔忘字的帝国 8418

Android-Date-picker:这是一个带有日期范围的android日期选择器对话框

官方资料: 4.3及更高版本) #DateTimePicker DateTimePicker是一个库,其中包含漂亮的DatePicker和TimePicker,可以在新的Google Agenda应用程序中看到它们。 该选择器可用于2.1+ 您在有一个具有相同样式的重复选择器。 警告 需要android-support-v4和 所有设备上的DatePicker都没有可访问性,而TimePicker上的ICS设备以下则缺少可访问性。 ICS设备下面缺少TimePicker和DatePicker的滚动调整。 描述 该库尽可能地复制了新的Google Agenda应用程序中包含的原始选择器。 试用示例APK 或浏览的获取完整的使用示例。 包括在您的项目中 最新版本是0.0.2 只需在build.gradle中添加以下语句 compile 'com.github.flavi

Android 应用系统的日期和时间选择器

为了解决这个问题,我向 TimePickerDialog 添加了一个对话框主题来设置按钮文本颜色。这是我添加到 styles.xml 的 xml 代码。三、遇到选择的取消和确认可以点击但是不显示的问题。由于某些原因按钮的字体颜色有时显示为白色。

Steve_XiaoHai的博客 1739

通义千问语言模型Qwen2.5架构详解

此外,将基于API的模型 Qwen-Plus 与领先的专有和开源模型进行了对比,包括 GPT4-o、Claude-3.5-Sonnet、Llama-3.1-405B和DeepSeek-V2.5。Qwen2.5-Math-72B-Instruct的整体性能超越了Qwen2-Math-72B-Instruct和GPT4-o,甚至是非常小的专业模型如 Qwen2.5-Math-1.5B-Instruct也能在与大型语言模型的竞争中取得高度竞争力的表现。近来也出现了明显的转向小型语言模型(SLMs)的趋势。

搏博的专栏 1万+

CentOS7 安装天气分型软件cost733过程详解

cost733是一款天气分型软件,其内置的一些算法可以帮助气象工作者进行天气类型划分等(我也是初学者,仅仅知道一些皮毛),但是网上找到的安装办法比较少,并且总会出现一些报错,试了一晚上,终于将安装办法调通了,希望可以帮助到大家

weixin_62573024的博客 3581

自定义Android日期选择器

然而系统提供的NumerPicker对外可以调用的接口(6.0版本上)非常少,只有预设范围等,根本不支持我们自定义布局。轻量级自定义的话(继承NumberPicker),也可以完成部分需求,比如修改主题色,只显示年月,修改字体颜色大小等等,但是一些比如选中时的背景之类的需求是完成不了的。 综上,我们只能通过重量级自定义来完成,也就是完全写一个新的类似于NumberPicker的控件,然后去绘制选中时的背景、文字字体颜色大小。

Hogwarts 4413

以GD32F103C8T6为例的核心板原理图PCB绘制学习笔记简单总结

重点在supplier part是立创商城的编号,可以搜到。

2301_80397661的博客 3294

Android基于wheelView实现自定义日期选择器

本文实例为大家分享了Android实现自定义日期选择器的具体代码,供大家参考,具体内容如下项目要求效果图:要求 “6月20 星期五” 这一项作为一个整体可以滑动,”7时”、”48分”分别作为一个滑动整体。系统自带的DatePicker、TimePicker大家都知道,只有这种效果:百度了很多,试了NumberPicker等...

weixin_40666052的博客 480

告别FreeRTOS?手把手带你用NuttX在STM32F4-Discovery上跑起第一个Shell

本文详细介绍了如何在STM32F4-Discovery开发板上从FreeRTOS迁移到NuttX,并运行第一个Shell。NuttX以其完整的POSIX兼容性和模块化架构优势,特别适合复杂网络应用的开发。通过环境搭建、移植配置、调试技巧及POSIX开发实践,帮助开发者高效构建嵌入式系统。

weixin_42526417的博客 305

车载以太网:PHY(物理层)介绍

本文以TJA1101芯片为实例介绍了车载以太网PHY(物理层)组成。

kevin1499的博客 6247

Android自定义DataTimePicker(日期选择器)

Android自定义DataTimePicker(日期选择器)

ytffhew的博客 352

【007】C++编程必知:原码、补码、反码的全面解读

计算机存储的是数据的补码。原码:计算机中对数字的二进制定点表示方法。比如123的原码是0111 1011。无符号数:原码=反码=补码。有符号数:要分正数和负数,正数的最高位为0,负数的最高位为1。其中,正数的原码、反码、补码都是相同的;负数的反码等于原码的符号数(最高位)不变,其他位取反;补码等于反码+1。比如:-123的原码是1111 1011,反码为1000 0100,补码为1000 0101。原码、补码和反码都是用于计算机表示有符号整数的方法。

Lion_Long的博客 2926

Android自定义WheelView滚轮,并在此基础上自定义日期选择器

项目需求,需要根据UI自定义日期选择器(Android自带的DatePicker极丑)。 所以自定义了WheelView,并在此基础上自定义日期选择器。 参考1.WheelView TimePicker CityPicker 滚轮(联动)选择器         2.使用第三方WheelView制作日期选择器         3.WheelPicker(推荐参考,功能强大) 自定义Whee...

u014611408的博客 1515

从 Open SQL JOIN 走向 ABAP CDS Association:把查询写成可复用的数据模型

摘要: 本文通过飞行模型示例,对比了ABAP中三种表关联实现方式:Open SQL JOIN、CDS显式JOIN和CDS Association。重点解析了Association的本质是将表关系声明为数据模型语言,使用时通过路径表达式按需展开为JOIN。文章指出路径表达式在不同位置会默认展开为不同JOIN类型(如SELECT列表默认为LEFT OUTER),并演示了如何用[inner]强制指定INNER JOIN。最后结合S/4HANA实际案例,说明Association通过中心化关系定义和路径复用,能有效

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 391

PyCharm 2024最新版图文安装教程(python环境搭建+PyCharm安装+运行测试+汉化+背景图设置)

PyCharm 2024新版图文安装教程(python环境搭建+PyCharm安装+运行测试+汉化+背景图设置)

Code_流苏:在代码中寻诗意,在实践中觅真知 4万+

《Android开发卷——自定义日期选择器(二)》

(小米手机) (中兴手机) 在上一篇中,我介绍了一般公司都会自定义时间日期选择器,并结合自己所做的项目给大家参考。 工作实录之《Android开发卷——自定义日期选择器(一)》链接:http://blog.csdn.net/chillax_li/article/details/19047913 这里,我在之前的基础上做了修改,弄成对话框形式,更神似于Google提供的日期选择器API,项

K. 5340

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Android中自定义日期选择器样式DatePickerDialog

res文件下value文件下style.xml。res文件下value文件下style.xml。

aidou1314的博客 299

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

《Android开发卷——自定义日期选择器(一)》

(小米手机)(中兴手机) 在实际开发中,Google官方提供的时间选择器API已经不能满足于我们的需要了,所以很多公司都是采用自定义的形式来实现日期选择器。 这个例子很简单,定义三个NumberPicker即可。 /NumberPicker android:id="@+id/np3" android:layout_width="80dp" a...

201

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

滚动时间选择器recyclerview_Android自定义wheelview实现滚动日期选择器

本文实例为大家分享了Android实现滚动日期选择器的具体代码,供大家参考,具体内容如下wheelview滚动效果的View这段时间需要用到一个时间选择器,但是不能使用日期对话框,因为它是筛选条件框架下的,只能是View!这个WheelView改造后可以达到要求!这个wheelview框架使用的类不多,就几个,还有一些资源文件。我根据这个框架设计了日期的选择器。主页面:第一种日期选择器页面:动态效...

weixin_39588679的博客 632

《Android开发卷——自定义日期选择器(三)》

继 《Android开发卷——自定义日期选择器(一)》:http://blog.csdn.net/chillax_li/article/details/19047913 《Android开发卷——自定义日期选择器(二)》:http://blog.csdn.net/chillax_li/article/details/19172899 之后,...

185

android自定义滚动选择日期,Android自定义wheelview实现滚动日期选择器

本文实例为大家分享了Android实现滚动日期选择器的具体代码,供大家参考,具体内容如下wheelview滚动效果的View这段时间需要用到一个时间选择器,但是不能使用日期对话框,因为它是筛选条件框架下的,只能是View!这个WheelView改造后可以达到要求!这个wheelview框架使用的类不多,就几个,还有一些资源文件。我根据这个框架设计了日期的选择器。主页面:第一种日期选择器页面:动态效...

weixin_34901388的博客 764

安卓代码选择日历图像功能java,Android自定义DataTimePicker实例代码(日期选择器)...

笔者有一段时间没有发表关于Android的文章了,关于Android自定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中。本篇博客给大家分享的是自定义一个日期选择器,可以让用户同时选择年月日和当前时间。先看看效果:实现的效果就是在同一个布局上显示日期选择和时间选择。自定义一个类:/DateTimePicker/src/com/wwj/datetimepicker/DateTimePickDia...

weixin_39581571的博客 175

Android轻松实现日期选择器、生日选择器、自定义起始时间

前言 Android轻松实现日期选择器、生日选择器、自定义起始时间 废话不多说 看下效果 效果图 代码实现 代码实现比较简单 按照步骤 你也可以实现同样的效果 第一步 设置依赖 android 和androidX都可以 //时间选择器 implementation 'com.contrarywind:Android-PickerView:3.2.7' 第二步 创建对象 创建 TimePickerView 对象 private TimePickerView pvTime; //时间选择器

吕氏春秋i 4751
上一篇: Android省市县三级联动 真实项目抽出 调用只需3行代码 源码免积分下载
下一篇: Android多选列表
源于未知
博客等级 码龄11年 21粉丝 111原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值