欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-04 19:02:52
android日期选择控件效果如下: 调用的代码: @onclick(r.id.btn0) public void btn0() { final a...

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

<?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();
 }
 }
}

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