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

Android仿ios年龄、生日、性别滚轮效果

程序员文章站 2022-05-16 12:29:49
新项目android和ios要做成统一样式,年龄,性别,时间,要做成滚轮效果,android没有原生控件,只能自己定义,于是我较劲脑汁,终于写出来,本着分享精神,贴出部分代...

新项目android和ios要做成统一样式,年龄,性别,时间,要做成滚轮效果,android没有原生控件,只能自己定义,于是我较劲脑汁,终于写出来,本着分享精神,贴出部分代码,直接拷贝就能用。

先看效果图,如果符合你的需求,再采纳

时间

Android仿ios年龄、生日、性别滚轮效果

年龄

Android仿ios年龄、生日、性别滚轮效果

性别

Android仿ios年龄、生日、性别滚轮效果

废话不多说,直接上代码

布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<relativelayout
 android:id="@+id/activity_main"
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingbottom="@dimen/activity_vertical_margin"
 android:paddingleft="@dimen/activity_horizontal_margin"
 android:paddingright="@dimen/activity_horizontal_margin"
 android:paddingtop="@dimen/activity_vertical_margin"
 >
 
 <textview
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="hello world!"
 android:id="@+id/textview"/>
 
 <button
 android:text="生日"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textview"
 android:layout_alignparentleft="true"
 android:layout_alignparentstart="true"
 android:id="@+id/button"
 android:onclick="date"
 android:layout_alignparentright="true"
 android:layout_alignparentend="true"/>
 
 <button
 android:text="时间"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button"
 android:layout_alignparentleft="true"
 android:layout_alignparentstart="true"
 android:id="@+id/button2"
 android:onclick="time"
 android:layout_alignparentright="true"
 android:layout_alignparentend="true"/>
 
 <button
 android:text="性别"
 android:onclick="zidingyi"
 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button2"
 android:layout_alignparentleft="true"
 android:layout_alignparentstart="true"
 android:id="@+id/button3"
 android:layout_alignparentright="true"
 android:layout_alignparentend="true"/>
</relativelayout>

mainactivity

public class mainactivity extends appcompatactivity {
 
 private dialog datedialog, timedialog, choosedialog;
 private textview mtextview;
 private list<string> list = new arraylist<>();
 
 
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 mtextview = (textview) findviewbyid(r.id.textview);
 string[] data = getresources().getstringarray(r.array.list);
 for (string str : data) {
  list.add(str);
 }
 }
 
 /**
 * choosedialog
 */
 private void showchoosedialog(list<string> mlist) {
 datapickerdialog.builder builder = new datapickerdialog.builder(this);
 choosedialog = builder.setdata(mlist).setselection(1).settitle("取消")
  .setondataselectedlistener(new datapickerdialog.ondataselectedlistener() {
   @override
   public void ondataselected(string itemvalue, int position) {
   mtextview.settext(itemvalue);
 
   }
 
   @override
   public void oncancel() {
 
   }
  }).create();
 
 choosedialog.show();
 }
 
 
 private void showdatedialog(list<integer> date) {
 datepickerdialog.builder builder = new datepickerdialog.builder(this);
 builder.setondateselectedlistener(new datepickerdialog.ondateselectedlistener() {
  @override
  public void ondateselected(int[] dates) {
 
  mtextview.settext(dates[0] + "-" + (dates[1] > 9 ? dates[1] : ("0" + dates[1])) + "-"
   + (dates[2] > 9 ? dates[2] : ("0" + dates[2])));
 
  }
 
  @override
  public void oncancel() {
 
  }
 })
 
  .setselectyear(date.get(0) - 1)
  .setselectmonth(date.get(1) - 1)
  .setselectday(date.get(2) - 1);
 
 builder.setmaxyear(dateutil.getyear());
 builder.setmaxmonth(dateutil.getdateforstring(dateutil.gettoday()).get(1));
 builder.setmaxday(dateutil.getdateforstring(dateutil.gettoday()).get(2));
 datedialog = builder.create();
 datedialog.show();
 }
 
 private void showtimepick() {
 
 if (timedialog == null) {
 
  timepickerdialog.builder builder = new timepickerdialog.builder(this);
  timedialog = builder.setontimeselectedlistener(new timepickerdialog.ontimeselectedlistener() {
  @override
  public void ontimeselected(int[] times) {
 
   mtextview.settext(times[0] + ":" + times[1]);
 
  }
  }).create();
 }
 
 timedialog.show();
 
 }
 
 
 public void time(view v) {
 
 showtimepick();
 
 }
 
 public void date(view v) {
 showdatedialog(dateutil.getdateforstring("1990-01-01"));
 
 }
 
 public void zidingyi(view v) {
 
 showchoosedialog(list);
 
 }
}

datapickerdialog

public class datapickerdialog extends dialog {
 
 private params params;
 
 public datapickerdialog(context context, int themeresid) {
 super(context, themeresid);
 }
 
 private void setparams(datapickerdialog.params params) {
 this.params = params;
 }
 
 
 public void setselection(string itemvalue) {
 if (params.datalist.size() > 0) {
  int idx = params.datalist.indexof(itemvalue);
  if (idx >= 0) {
  params.initselection = idx;
  params.loopdata.setcurrentitem(params.initselection);
  }
 }
 }
 
 public interface ondataselectedlistener {
 void ondataselected(string itemvalue, int position);
 void oncancel();
 }
 
 private static final class params {
 private boolean shadow = true;
 private boolean cancancel = true;
 private loopview loopdata;
 private string title;
 private string unit;
 private int initselection;
 private ondataselectedlistener callback;
 private final list<string> datalist = new arraylist<>();
 }
 
 public static class builder {
 private final context context;
 private final datapickerdialog.params params;
 
 public builder(context context) {
  this.context = context;
  params = new datapickerdialog.params();
 }
 
 private final string getcurrdatevalue() {
  return params.loopdata.getcurrentitemvalue();
 }
 
 public builder setdata(list<string> datalist) {
  params.datalist.clear();
  params.datalist.addall(datalist);
  return this;
 }
 
 public builder settitle(string title) {
  params.title = title;
  return this;
 }
 
 public builder setunit(string unit) {
  params.unit = unit;
  return this;
 }
 
 public builder setselection(int selection) {
  params.initselection = selection;
  return this;
 }
 
 public builder setondataselectedlistener(ondataselectedlistener ondataselectedlistener) {
  params.callback = ondataselectedlistener;
  return this;
 }
 
 
 public datapickerdialog create() {
  final datapickerdialog dialog = new datapickerdialog(context, params.shadow ? r.style.theme_light_notitle_dialog : r.style.theme_light_notitle_noshadow_dialog);
  view view = layoutinflater.from(context).inflate(r.layout.layout_picker_data, null);
 
  if (!textutils.isempty(params.title)) {
  textview txtitle = (textview) view.findviewbyid(r.id.tx_title);
  txtitle.settext(params.title);
  txtitle.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view view) {
   dialog.dismiss();
   params.callback.oncancel();
   }
  });
  }
  if (!textutils.isempty(params.unit)) {
  textview txunit = (textview) view.findviewbyid(r.id.tx_unit);
  txunit.settext(params.unit);
  }
 
  final loopview loopdata = (loopview) view.findviewbyid(r.id.loop_data);
  loopdata.setarraylist(params.datalist);
  loopdata.setnotloop();
  if (params.datalist.size() > 0) loopdata.setcurrentitem(params.initselection);
  view.findviewbyid(r.id.tx_finish).setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
   dialog.dismiss();
   params.callback.ondataselected(getcurrdatevalue(),loopdata.getcurrentitem());
  }
  });
 
  window win = dialog.getwindow();
  win.getdecorview().setpadding(0, 0, 0, 0);
  windowmanager.layoutparams lp = win.getattributes();
  lp.width = windowmanager.layoutparams.match_parent;
  lp.height = windowmanager.layoutparams.wrap_content;
  win.setattributes(lp);
  win.setgravity(gravity.bottom);
  win.setwindowanimations(r.style.animation_bottom_rising);
 
  dialog.setcontentview(view);
  dialog.setcanceledontouchoutside(params.cancancel);
  dialog.setcancelable(params.cancancel);
 
  params.loopdata = loopdata;
  dialog.setparams(params);
 
  return dialog;
 }
 }
}

datepickerdialog

public class datepickerdialog extends dialog {
 
 private static int min_year = 1900;
 private static int max_year = 2100;
 private params params;
 
 public datepickerdialog(context context, int themeresid) {
 super(context, themeresid);
 }
 
 private void setparams(datepickerdialog.params params) {
 this.params = params;
 }
 
 public interface ondateselectedlistener {
  void ondateselected(int[] dates);
  void oncancel();
 }
 
 
 
 private static final class params {
 private boolean shadow = true;
 private boolean cancancel = true;
 private loopview loopyear, loopmonth, loopday;
 private ondateselectedlistener callback;
 }
 
 public static class builder {
 private final context context;
 private final datepickerdialog.params params;
 private integer minyear;
 private integer maxyear;
 private integer selectyear;
 private integer selectmonth;
 private integer selectday;
 private integer minmonth;
 private integer maxmonth;
 private integer minday;
 private integer maxday;
 
 public builder(context context) {
  this.context = context;
  params = new datepickerdialog.params();
 }
 
 public builder setminyear(int year){
  minyear=year;
  return this;
 }
 
 public builder setmaxyear(int year){
  maxyear=year;
  return this;
 }
 
 public builder setminmonth(int month){
  minmonth=month;
  return this;
 }
 
 public builder setmaxmonth(int month){
  maxmonth=month;
  return this;
 }
 
 public builder setminday(int day){
  minday=day;
  return this;
 }
 
 public builder setmaxday(int day){
  maxday=day;
  return this;
 }
 
 public builder setselectyear(int year){
  this.selectyear=year;
  return this;
 }
 
 public builder setselectmonth(int month){
  this.selectmonth=month;
  return this;
 }
 
 public builder setselectday(int day){
  this.selectday=day;
  return this;
 }
 
 /**
  * 获取当前选择的日期
  *
  * @return int[]数组形式返回。例[1990,6,15]
  */
 private final int[] getcurrdatevalues() {
  int curryear = integer.parseint(params.loopyear.getcurrentitemvalue());
  int currmonth = integer.parseint(params.loopmonth.getcurrentitemvalue());
  int currday = integer.parseint(params.loopday.getcurrentitemvalue());
  return new int[]{curryear, currmonth, currday};
 }
 
 public builder setondateselectedlistener(ondateselectedlistener ondateselectedlistener) {
  params.callback = ondateselectedlistener;
  return this;
 }
 
 
 public datepickerdialog create() {
  final datepickerdialog dialog = new datepickerdialog(context, params.shadow ? r.style.theme_light_notitle_dialog : r.style.theme_light_notitle_noshadow_dialog);
  view view = layoutinflater.from(context).inflate(r.layout.layout_picker_date, null);
 
  view.findviewbyid(r.id.tv_cancel).setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view view) {
   dialog.dismiss();
   params.callback.oncancel();
  }
  });
 
 
 
  calendar c = calendar.getinstance();
 
  final loopview loopday = (loopview) view.findviewbyid(r.id.loop_day);
  loopday.setarraylist(d(1, 30));
  if(selectday!=null){
  loopday.setcurrentitem(selectday);
  }else{
  loopday.setcurrentitem(c.get(calendar.date));
  }
  //loopday.setnotloop();
 
  final loopview loopyear = (loopview) view.findviewbyid(r.id.loop_year);
  loopyear.setarraylist(d(min_year, max_year - min_year + 1));
  if(selectyear!=null){
  loopyear.setcurrentitem(selectyear-min_year+1);
  }else{
  loopyear.setcurrentitem(max_year);
  }
  loopyear.setnotloop();
 
  final loopview loopmonth = (loopview) view.findviewbyid(r.id.loop_month);
  loopmonth.setarraylist(d(1, 12));
  if(selectmonth!=null){
  loopmonth.setcurrentitem(selectmonth);
  }else{
  loopmonth.setcurrentitem(c.get(calendar.month));
  }
  loopmonth.setnotloop();
 
 
 
  final looplistener maxdaysynclistener = new looplistener() {
  @override
  public void onitemselect(int item) {
   calendar c = calendar.getinstance();
   boolean needfixed=true;
   if(minyear!=null){
   if(integer.parseint(loopyear.getcurrentitemvalue())==minyear ){
    if(minmonth!=null){
    if(integer.parseint(loopmonth.getcurrentitemvalue())<minmonth){
     loopmonth.setcurrentitem(minmonth - 1);
    }
    }
   }else if(integer.parseint(loopyear.getcurrentitemvalue())<minyear){
    loopyear.setcurrentitem(minyear-min_year);
   }
   }
 
   if(maxyear!=null){
   if(integer.parseint(loopyear.getcurrentitemvalue())==maxyear ){
    if(maxmonth!=null){
    if(integer.parseint(loopmonth.getcurrentitemvalue())>maxmonth){
     loopmonth.setcurrentitem(maxmonth - 1);
    }
    }
   }else if(integer.parseint(loopyear.getcurrentitemvalue())>maxyear){
    loopyear.setcurrentitem(maxyear-min_year);
   }
   }
 
   c.set(integer.parseint(loopyear.getcurrentitemvalue()), integer.parseint(loopmonth.getcurrentitemvalue()) - 1, 1);
   c.roll(calendar.date, false);
 
   if(needfixed){
   int maxdayofmonth = c.get(calendar.date);
   int fixedcurr = loopday.getcurrentitem();
   loopday.setarraylist(d(1, maxdayofmonth));
   // 修正被选中的日期最大值
   if (fixedcurr > maxdayofmonth) fixedcurr = maxdayofmonth - 1;
   loopday.setcurrentitem(fixedcurr);
   }
  }
  };
 
  final looplistener daylooplistener=new looplistener() {
  @override
  public void onitemselect(int item) {
   if(minyear!=null && minmonth!=null && minday!=null
    && integer.parseint(loopyear.getcurrentitemvalue())==minyear
    && integer.parseint(loopmonth.getcurrentitemvalue())==minmonth
    && integer.parseint(loopday.getcurrentitemvalue())<minday
    ){
   loopday.setcurrentitem(minday-1);
   }
 
   if(maxyear!=null && maxmonth!=null && maxday!=null
    && integer.parseint(loopyear.getcurrentitemvalue())==maxyear
    && integer.parseint(loopmonth.getcurrentitemvalue())==maxmonth
    && integer.parseint(loopday.getcurrentitemvalue())>maxday
    ){
   loopday.setcurrentitem(maxday-1);
   }
  }
  };
  loopyear.setlistener(maxdaysynclistener);
  loopmonth.setlistener(maxdaysynclistener);
  loopday.setlistener(daylooplistener);
 
  view.findviewbyid(r.id.tx_finish).setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
   dialog.dismiss();
   params.callback.ondateselected(getcurrdatevalues());
  }
  });
 
  window win = dialog.getwindow();
  win.getdecorview().setpadding(0, 0, 0, 0);
  windowmanager.layoutparams lp = win.getattributes();
  lp.width = windowmanager.layoutparams.match_parent;
  lp.height = windowmanager.layoutparams.wrap_content;
  win.setattributes(lp);
  win.setgravity(gravity.bottom);
  win.setwindowanimations(r.style.animation_bottom_rising);
 
  dialog.setcontentview(view);
  dialog.setcanceledontouchoutside(params.cancancel);
  dialog.setcancelable(params.cancancel);
 
  params.loopyear = loopyear;
  params.loopmonth = loopmonth;
  params.loopday = loopday;
  dialog.setparams(params);
 
  return dialog;
 }
 
 /**
  * 将数字传化为集合,并且补充0
  *
  * @param startnum 数字起点
  * @param count 数字个数
  * @return
  */
 private static list<string> d(int startnum, int count) {
  string[] values = new string[count];
  for (int i = startnum; i < startnum + count; i++) {
  string tempvalue = (i < 10 ? "0" : "") + i;
  values[i - startnum] = tempvalue;
  }
  return arrays.aslist(values);
 }
 
 }
}

代码太多就不一一贴出来了,有需要直接下载:完整demo地址

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