当前位置:  首页  >  PHP资讯  >  业界资讯

Android自定义控件之日期选择控件使用详解

这篇文章主要为大家详细介绍了Android自定义控件之日期选择控件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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布局文件

 <&#63;xml version="1.0" encoding="utf-8"&#63;>        
 <&#63;xml version="1.0" encoding="utf-8"&#63;>              

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 yearList = new ArrayList<>(YEAR_MAX - YEAR_MIN + 1); private ArrayList mOnthList= new ArrayList<>(12); private ArrayList 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  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 arrayList = adapter.getTextViews(); int size = arrayList.size(); String currentText; TextView textView; boolean current; for (int i = 0; i  YEAR_MAX || year  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 getDayList(int days) { if (days <= 0) { return null; } ArrayList list = new ArrayList(days); for (int i = 1; i  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 内容如下 // <&#63;xml version="1.0" encoding="utf-8"&#63;> //  //  //  } public void setList(ArrayList list) { this.list = list; notifyDataChangedEvent(); } @Override protected CharSequence getItemText(int index) { return list == null &#63; "" : String.valueOf(list.get(index)); } @Override public int getItemsCount() { return list == null &#63; 0 : list.size(); } } } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

内容推荐:免费高清PNG素材下载
吐了个 "CAO" !
扫码关注 PHP1 官方微信号
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有 京ICP备19059560号-4