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] + "日"); } }); }
1.WheelVIEw 源码(有修改)
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,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 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,getSelectDate()); } } }); wheelVIEwDay.addChangingListener(new OnWheelChangedListener() { @OverrIDe public voID onChanged(WheelVIEw wheel,int newValue) { day = 1 + newValue; if (onSelectedChangedListener != null) { onSelectedChangedListener.OnSelectedChanged(parseInt2Array(year,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 day) { return new int[]{year,day}; } private int[] getSelectDate() { return new int[]{year,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自定义控件之日期选择控件使用详解全部内容,希望文章能够帮你解决Android自定义控件之日期选择控件使用详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)