# TimePicker
时间与日期选择控件
# Android 自定义时间滑轮选择控件
刚开始项目中使用原生的控件,5.0的控件可以说很炫酷,很漂亮,很是喜欢,不过客户反应用不来,太麻烦~,好吧原谅你一次~。
所以为了客户的着想使用4.0的那种时间选择控件,可是大家都懂的,控件简单暴力,就是太难看了,呜呜~
so~ 为了给整个APP搭配,选择以4.0的控件为基础进行改进自定义,让它美美哒~
下面大家可以看图对比下。
这个是原生的图
![原生](http://i.imgur.com/Ya3QV5k.png)
经过改进美化的图,而且可以自定义标题~
![](https://github.com/ssyandroid/TimePicker/raw/master/time.png)
![](https://github.com/ssyandroid/TimePicker/raw/master/date.png)
是不是好看多了,并且支持自定义!
也就是说这个控件只是一个继承AlertDialog自定义的。
主要的滑轮还是使用的是NumberPicker,用了它基本上都能满足自定义的滑轮选择控件。
不过要注意的是,直接采用原生的NumberPicker可能无法达到效果,因为原生的直接改动很小,所以我们是直接把原生的代码拷贝一份,重新自定义自己的NumberPicker,只需改动一点点议即可~
直接进入主题:
>- 使用原生的NumberPicker自定义自己需要的NumberPicker
>- 实现MyTimePickerDialog继承AlertDialog进行自定义
>- 代码使用,使用方法当然要给原生的差不多啦~
首先使用原生的NumberPicker自定义自己需要的NumberPicker,大家可以在API源代码了找到NumberPicker类,进行复制粘贴到自己项目工程里。
当然要找API23以下的,我看25的已经变化了,在android.widget里很容易找到的~,找不到源码的自行谷歌哈,当然,亦可以使用我的,全代码在后面会备上的~
直接复制过来,肯定有的方法会报错,因为找不到方法,那也很简单,一是看看该方法是什么作用,重要不重要,如果说的该方法可以不要对项目无影响,则进行直接处理错误,如果有用,可以找个替代的方法补替即可。
同理,大家可以根据自己的需求自己写一个,或者谷歌一个适合自己的。适合自己的才是最好的,大家灵活变通啦~
但是要注意我们要写上它的属性配置,在你的attrs文件里写上,大家可以找源代码里的属性,我这里直接谷歌的:
<!-- Don't touch this -->
<attr name="numberPickerStyle" format="reference" />
<declare-styleable name="NumberPicker">
<!-- @hide Color for the solid color background if such for optimized rendering. -->
<attr name="solidColor" format="color|reference" />
<!-- @hide The divider for making the selection area. -->
<attr name="selectionDivider" format="reference" />
<!-- @hide The height of the selection divider. -->
<attr name="selectionDividerHeight" format="dimension" />
<!-- @hide The distance between the two selection dividers. -->
<attr name="selectionDividersDistance" format="dimension" />
<!-- @hide The min height of the NumberPicker. -->
<attr name="internalMinHeight" format="dimension" />
<!-- @hide The max height of the NumberPicker. -->
<attr name="internalMaxHeight" format="dimension" />
<!-- @hide The min width of the NumberPicker. -->
<attr name="internalMinWidth" format="dimension" />
<!-- @hide The max width of the NumberPicker. -->
<attr name="internalMaxWidth" format="dimension" />
<!-- @hide The layout of the number picker. -->
<attr name="internalLayout" format="reference" />
<!-- @hide The drawable for pressed virtual (increment/decrement) buttons. -->
<attr name="virtualButtonPressedDrawable" format="reference" />
</declare-styleable>
然后还要在主题文件里设置这些属性以满足自己需求哦。下面是我的仅供参考:
<style name="NPWidget">
<item name="android:textAppearance">?android:attr/textAppearance</item>
</style>
<style name="NPWidget.NumberPicker">
<item name="android:orientation">vertical</item>
<item name="android:fadingEdge">vertical</item>
<item name="android:fadingEdgeLength">50dip</item>
</style>
<!--NumberPicker style-->
<style name="NPWidget.Holo.NumberPicker" parent="NPWidget.NumberPicker">
<item name="solidColor">@android:color/transparent</item>
<item name="selectionDivider">@color/actionbarcolor_press</item>
<item name="selectionDividerHeight">2dip</item>
<item name="internalLayout">@layout/number_picker_with_selector_wheel</item>
<item name="internalMinWidth">50dip</item>
<item name="internalMaxHeight">150dip</item>
<!--<item name="virtualButtonPressedDrawable">@drawable/item_background_holo_dark</item>-->
</style>
这样大家就完成自己的NumberPicker,然后需要到NumberPicker都已直接使用的~
比如直接xml里使用:
<com.sansan.widget.NumberPicker
android:id="@+id/minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true" />
这就完成了第一步。
### 第二步 实现MyTimePickerDialog继承AlertDialog进行自定义 ###
这个大家都会,和简单,主要就是自定义布局,与实现自己业务与需求。
在此就简单叙述下,大家先看下我的:
/**
* Creates a new time picker dialog with the specified theme.
*
* @param context the parent context
* @param listener the listener to call when the time is set
* @param dateTime 时间比如12:01,一定要这个格式,如果为空则为默认当前时间
* @param hourOfDay the initial hour
* @param minute the initial minute
*/
private MyTimePickerDialog(Context context, String title, OnTimeSetListener listener, String dateTime, int hourOfDay, int minute) {
super(context);
//设置对话框标题,没有就默认
if (!TextUtils.isEmpty(title)) {
setTitle(title);
}
mTimeSetListener = listener;
//这个是显示自己设置的时间,没有就是默认当前时间
if (hourOfDay < 0 || minute < 0) {
if (TextUtils.isEmpty(dateTime)) {
//初始化时间
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
} else {
try {
hourOfDay = Integer.valueOf(dateTime.substring(0, 2));
minute = Integer.valueOf(dateTime.substring(3, 5));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
final Context themeContext = getContext();
//加载自定义布局
final LayoutInflater inflater = LayoutInflater.from(themeContext);
view = inflater.inflate(R.layout.time_picker_dialog, null);
setView(view);
setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
mHour = (NumberPicker) view.findViewById(R.id.hour);
mMinute = (NumberPicker) view.findViewById(R.id.minute);
//使用NumberPicker里的方法设置最大,最小值
mHour.setMaxValue(23);
mHour.setMinValue(0);
mHour.setFocusable(true);
mHour.setFocusableInTouchMode(true);
mHour.setFormatter(this);
mHour.setValue(hourOfDay);
mHour.setOnValueChangedListener(this);
mMinute.setMaxValue
没有合适的资源?快使用搜索试试~ 我知道了~
时间选择器Android源码
共45个文件
xml:19个
png:7个
java:6个
需积分: 0 1 下载量 173 浏览量
2023-01-27
21:32:35
上传
评论
收藏 187KB ZIP 举报
温馨提示
时间选择器Android源码
资源推荐
资源详情
资源评论
收起资源包目录
TimePicker.zip (45个子文件)
TimePicker-master
date.png 36KB
MyApplication
gradle.properties 730B
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 233B
app
src
androidTest
java
com
sansan
myapplication
ExampleInstrumentedTest.java 752B
test
java
com
sansan
myapplication
ExampleUnitTest.java 402B
main
java
com
sansan
myapplication
MainActivity.java 1KB
widget
NumberPicker.java 102KB
MyTimePickerDialog.java 9KB
MyDatePickerDialog.java 12KB
res
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-hdpi
ic_launcher.png 3KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xxxhdpi
ic_launcher.png 10KB
mipmap-xhdpi
ic_launcher.png 5KB
values-w820dp
dimens.xml 358B
values
dimens.xml 211B
colors.xml 208B
ids.xml 240B
strings.xml 77B
attrs.xml 1KB
styles.xml 1KB
layout
number_picker_with_selector_wheel.xml 1KB
time_picker_dialog.xml 2KB
activity_main.xml 962B
date_picker_dialog.xml 3KB
AndroidManifest.xml 664B
proguard-rules.pro 645B
build.gradle 872B
.gitignore 7B
gradlew.bat 2KB
build.gradle 498B
.idea
runConfigurations.xml 564B
misc.xml 3KB
compiler.xml 727B
modules.xml 363B
gradle.xml 626B
encodings.xml 159B
copyright
profiles_settings.xml 74B
settings.gradle 15B
gradlew 5KB
.gitignore 118B
.gitattributes 378B
time.png 34KB
README.md 13KB
共 45 条
- 1
资源评论
weixin_38033175
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功