Android 自定义控件之 日期选择控件
效果如下:
调用的代码:
@OnClick(R.id.btn0)
public void btn0() {
final AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.show();
Window window = dialog.getWindow();
window.setContentView(R.layout.dialog_change_date);
window.setBackgroundDrawable(new ColorDrawable(0x00000000)); // 处理5.0以上对话框的白边问题
window.setGravity(Gravity.BOTTOM);
final DatePickerView datePickerView = (DatePickerView) window.findViewById(R.id.datePickerView); //打开页面时需要选中的日期 TODO
datePickerView.setDate(2015, 5, 11);
// datePickerView.setDate(birthdayArray[0], birthdayArray[1], birthdayArray[2]); final int[] birthdayArray = new int[3];
datePickerView.addOnSelectedChangingListener(new DatePickerView.OnSelectedChangedListener() {
@Override
public void OnSelectedChanged(int[] oldValue, int[] newValue) {
birthdayArray[0] = newValue[0];
birthdayArray[1] = newValue[1];
birthdayArray[2] = newValue[2];
}
});
window.findViewById(R.id.tvCancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
window.findViewById(R.id.tvOK).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss(); btn0.setText(birthdayArray[0] + "年" + birthdayArray[1] + "月" + birthdayArray[2] + "日");
}
}); }
2.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<!--widget_date_picker.xml-->
<!--注意修改页面自定义控件的包名-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <de.bvb.rxdemo.widget.DateSelectWidget.wheelview.WheelView
android:id="@+id/wheelViewYear"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"/> <de.bvb.rxdemo.widget.DateSelectWidget.wheelview.WheelView
android:id="@+id/wheelViewMonth"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"/> <de.bvb.rxdemo.widget.DateSelectWidget.wheelview.WheelView
android:id="@+id/wheelViewDay"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<!--dialog_change_date.xml-->
<!--注意修改页面自定义控件的包名-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:gravity="bottom"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"> <TextView
android:id="@+id/tvCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#F9F9F9"
android:gravity="center"
android:text="取消"
android:textColor="#43AAFC"/> <View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#D7D7D7"/> <TextView
android:id="@+id/tvOK"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#F9F9F9"
android:gravity="center"
android:text="确定"
android:textColor="#43AAFC"/> </LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#D7D7D7"/> <de.bvb.rxdemo.widget.DateSelectWidget.DatePickerView
android:id="@+id/datePickerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </LinearLayout>
</LinearLayout>
3.java文件
package de.bvb.rxdemo.widget.DateSelectWidget; import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView; import java.util.ArrayList; import de.bvb.rxdemo.R;
import de.bvb.rxdemo.widget.DateSelectWidget.wheelview.OnWheelChangedListener;
import de.bvb.rxdemo.widget.DateSelectWidget.wheelview.OnWheelScrollListener;
import de.bvb.rxdemo.widget.DateSelectWidget.wheelview.WheelView;
import de.bvb.rxdemo.widget.DateSelectWidget.wheelview.adapter.AbstractWheelTextAdapter1; public class DatePickerView extends LinearLayout { private static final int YEAR_MIN = 1950;
private static final int YEAR_MAX = 2020; private int year = YEAR_MIN;
private int month = 1;
private int day = 1; private ArrayList<Integer> yearList = new ArrayList<>(YEAR_MAX - YEAR_MIN + 1);
private ArrayList<Integer> monthList = new ArrayList<>(12);
private ArrayList<Integer> dayList = new ArrayList<>(31); private DateTextAdapter yearAdapter;
private DateTextAdapter monthAdapter;
private DateTextAdapter dayAdapter; private Context context;
private WheelView wheelViewYear;
private WheelView wheelViewMonth;
private WheelView wheelViewDay; public DatePickerView(Context context) {
this(context, null);
} public DatePickerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public DatePickerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
} private void init() {
for (int i = YEAR_MIN; i < YEAR_MAX + 1; i++) {
yearList.add(i);
}
for (int i = 1; i < 13; i++) {
monthList.add(i);
}
for (int i = 1; i < 32; i++) {
dayList.add(i);
} LayoutInflater.from(context).inflate(R.layout.widget_date_picker, this);
// View.inflate(context, R.layout.widget_date_picker, this); wheelViewYear = (WheelView) findViewById(R.id.wheelViewYear);
wheelViewMonth = (WheelView) findViewById(R.id.wheelViewMonth);
wheelViewDay = (WheelView) findViewById(R.id.wheelViewDay); wheelViewYear.setCyclic(true);// 可循环滚动
wheelViewMonth.setCyclic(true);// 可循环滚动
wheelViewDay.setCyclic(true);// 可循环滚动 yearAdapter = new DateTextAdapter(context);
monthAdapter = new DateTextAdapter(context);
dayAdapter = new DateTextAdapter(context); yearAdapter.setList(yearList);
monthAdapter.setList(monthList);
dayAdapter.setList(dayList); wheelViewYear.setViewAdapter(yearAdapter);
wheelViewMonth.setViewAdapter(monthAdapter);
wheelViewDay.setViewAdapter(dayAdapter); wheelViewYear.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
year = YEAR_MIN + newValue;
int days = calcDay(year, month); // days=28
dayList = getDayList(days);
dayAdapter.setList(dayList);
if (day > days) {
dayAdapter.currentIndex = days - 1;
wheelViewDay.setCurrentItem(dayAdapter.currentIndex);
} else {
dayAdapter.currentIndex = day - 1; // day = 30
} if (onSelectedChangedListener != null) {
onSelectedChangedListener.OnSelectedChanged(parseInt2Array(YEAR_MIN + oldValue, month, day), getSelectDate());
}
}
}); wheelViewMonth.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
month = 1 + newValue;
int days = calcDay(year, month); // days=28
dayList = getDayList(days);
dayAdapter.setList(dayList);
if (day > days) {
dayAdapter.currentIndex = days - 1;
wheelViewDay.setCurrentItem(dayAdapter.currentIndex);
} else {
dayAdapter.currentIndex = day - 1; // day = 30
} if (onSelectedChangedListener != null) {
onSelectedChangedListener.OnSelectedChanged(parseInt2Array(year, 1 + oldValue, day), getSelectDate());
}
}
}); wheelViewDay.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
day = 1 + newValue;
if (onSelectedChangedListener != null) {
onSelectedChangedListener.OnSelectedChanged(parseInt2Array(year, month, oldValue + 1), getSelectDate());
}
}
}); wheelViewYear.addScrollingListener(onWheelScrollListener);
wheelViewMonth.addScrollingListener(onWheelScrollListener);
wheelViewDay.addScrollingListener(onWheelScrollListener);
} OnWheelScrollListener onWheelScrollListener = new OnWheelScrollListener() {
@Override
public void onScrollingStarted(WheelView wheel) { } @Override
public void onScrollingFinished(WheelView wheel) {
setTextViewStyle();
}
}; private void setTextViewStyle() {
setTextViewSize(year + "", yearAdapter);
setTextViewSize(month + "", monthAdapter);
setTextViewSize(day + "", dayAdapter);
} private void setTextViewSize(String currentItemText, AbstractWheelTextAdapter1 adapter) {
ArrayList<View> arrayList = adapter.getTextViews();
int size = arrayList.size();
String currentText;
TextView textView;
boolean current;
for (int i = 0; i < size; i++) {
textView = (TextView) arrayList.get(i);
currentText = textView.getText().toString();
current = currentItemText.equals(currentText);
textView.setTextColor(current ? adapter.selected_text_color : adapter.un_selected_text_color);
textView.setTextSize(current ? adapter.selected_text_size : adapter.un_selected_text_size);
}
} /**
* 设置控件的初始值
*
* @param year
* @param month
* @param day
*/
public void setDate(int year, int month, int day) {
//验证合法性
if (year > YEAR_MAX || year < YEAR_MIN) {
year = YEAR_MIN;
// throw new RuntimeException("年份必须在[" + YEAR_MIN + "," + YEAR_MAX + "]之间");
}
if (month > 12 || month < 1) {
month = 1;
// throw new RuntimeException("月份份必须在[" + 1 + "," + 12 + "]之间");
}
final int days = calcDay(year, month);
if (day > days || day < 1) {
day = 1;
// throw new RuntimeException("日期份必须在[" + 1 + "," + days + "]之间");
} this.year = year;
this.month = month;
this.day = day; yearAdapter.currentIndex = DatePickerView.this.year - YEAR_MIN;
monthAdapter.currentIndex = DatePickerView.this.month - 1;
dayAdapter.currentIndex = DatePickerView.this.day - 1; wheelViewYear.setCurrentItem(yearAdapter.currentIndex);
wheelViewMonth.setCurrentItem(monthAdapter.currentIndex);
wheelViewDay.setCurrentItem(dayAdapter.currentIndex);
} public void addOnSelectedChangingListener(OnSelectedChangedListener onSelectedChangedListener) {
this.onSelectedChangedListener = onSelectedChangedListener;
} private OnSelectedChangedListener onSelectedChangedListener; public interface OnSelectedChangedListener {
void OnSelectedChanged(int[] oldValue, int[] newValue);
} private int[] parseInt2Array(int year, int month, int day) {
return new int[]{year, month, day};
} private int[] getSelectDate() {
return new int[]{year, month, day};
} private ArrayList<Integer> getDayList(int days) {
if (days <= 0) {
return null;
}
ArrayList<Integer> list = new ArrayList(days);
for (int i = 1; i < days + 1; i++) {
list.add(i);
}
return list;
} private int calcDay(int year, int month) {
int days = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) ? 29 : 28;
break;
}
return days;
} private class DateTextAdapter extends AbstractWheelTextAdapter1 {
ArrayList<Integer> list; public DateTextAdapter(Context context) {
super(context, android.R.layout.simple_list_item_1);
// super(context, R.layout.item_birth_year);
// setItemTextResource(R.id.tempValue); // item_birth_year.xml 内容如下
// <?xml version="1.0" encoding="utf-8"?>
// <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
// android:layout_width="match_parent"
// android:layout_height="35dip"
// android:gravity="center"
// android:orientation="horizontal">
// <TextView
// android:id="@+id/tempValue"
// android:layout_width="match_parent"
// android:layout_height="match_parent"
// android:gravity="center"
// android:textColor="#ffff0000"/>
// </LinearLayout>
} public void setList(ArrayList<Integer> list) {
this.list = list;
notifyDataChangedEvent();
} @Override
protected CharSequence getItemText(int index) {
return list == null ? "" : String.valueOf(list.get(index));
} @Override
public int getItemsCount() {
return list == null ? 0 : list.size();
}
}
}
Android 自定义控件之 日期选择控件的更多相关文章
- 用c/c++混合编程方式为ios/android实现一个自绘日期选择控件(一)
本文为原创,如有转载,请注明出处:http://www.cnblogs.com/jackybu 前言 章节: 1.需求描述以及c/c++实现日期和月历的基本操作 2.ios实现自绘日期选择控件 3.a ...
- Android自定义View(RollWeekView-炫酷的星期日期选择控件)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/53420889 本文出自:[openXu的博客] 目录: 1分析 2定义控件布局 3定义Cus ...
- Android自定义控件之自定义组合控件
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- js 跨域的问题 (同一个主域名不同的二级域名下的跨域问题) 解决 WdatePicker.js my97日期选择控件
例如域名是 a.xx.com 和 b.xx.com 如果一个页面中引入多个iframe,要想能够操作所有iframe,必须都得设置相同domain. 如果iframe的时候 a包含b 为 ...
- JQuery好用的日期选择控件 DatePicker
近期发现一个很好的基于JQ的前端UI日期选择控件Jquery.DatePicker.js 下载地址:jquery.DatePIcker.js 演示地址:DatePicker - 基于jQuery 1. ...
- [Ext JS 4] 实战之 带week(星期)的日期选择控件(三)
前言 在 [Ext JS 4] 实战之 带week(星期)的日期选择控件(二) 的最后,有提到一个解决方案. 不过这方案有一个条件 ==> “2. 每年的周数从(1-52), 如果超过52 周 ...
- Swift - 日期选择控件(UIDatePicker)的用法
1,使用storyboard创建日期选择控件 首先我们将一个UIDatePicker控件和一个按钮直接添加到Main.Storyboard上.该按钮是为了点击时弹出提示框显示当前选择的日期和时间. 同 ...
- 取消layUI中日期选择控件默认填充日期
input标签中使用日期选择控件填写,加载时默认填充当前日期, 标签设置了placeholder="请选择" autocomplete="off",但是并没有效 ...
- Ionic实战六:日期选择控件
onic日期选择控件,用于ionic项目开发中的日期选择以及日期插件   
随机推荐
- github又提交不了代码了..... X_X
如下: 我们使用git提交代码过程中,突然就登录不上了 原因是 用户名被更改了:git@gitlab.0easy.com 是你的用户名 造成的原因是: 我们clone代码过程中选择了SSH的地址 解决 ...
- 淘宝npm镜像安装失败的问题
一:背景 心血来潮要简单搞一搞前端运行.打包的东西.结果第一步通过npm安装淘宝npm的时候就出问题了,如图: 二:解决方法 图片显示有点垃圾,但是看出来“Missing write access t ...
- 005-tomcat日志体系
一.概述 首先了解java的日志体系 在JDK1.4后,sun公司增加了一个包为java.util.logging,简称为jul,用以对抗log4j. 后续还有很多日志门面方案,但是tomcat使用了 ...
- osg 在场景中绘制坐标轴(xyz)
//x y z font_size osg::Geode* makeCoordinate(float a_x,float a_y,float a_z,float font_size) { osg::r ...
- ABAP Memory ID
转自:https://blog.csdn.net/lyq123333321/article/details/52659114 (一) Difference Between SAP a ...
- Intellij-编译
目录 IntelliJ IDEA 编译方式介绍 编译方式介绍 编译触发按钮 运行之前的编译 @(目录) IntelliJ IDEA 编译方式介绍 编译方式介绍 相比较于 Eclipse 的实时自动编译 ...
- laravel5.2总结
转自:http://www.cnblogs.com/redirect/p/6178001.html
- web前端学习路程
学习路程: 1.HTML和CSS基础 2.JavaScript语言 3.jQuery and ajax 4.综合网站实践 5.优化及调试
- rsync参数说明
参数说明: log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建 pidfile = /var/run/rsyncd.pid ...
- 最新 珍岛java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.珍岛等10家互联网公司的校招Offer,因为某些自身原因最终选择了珍岛.6.7月主要是做系统复习.项目复盘.LeetCode ...