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

Android仿淘宝预订日历(18)

程序员文章站 2023-12-20 13:19:10
本文实例为大家分享了android实现淘宝预订日历的具体代码,供大家参考,具体内容如下 mainactivity.java代码: package siso.ca...

本文实例为大家分享了android实现淘宝预订日历的具体代码,供大家参考,具体内容如下

mainactivity.java代码:

package siso.calendarselector;

import android.app.activity;
import android.content.intent;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.edittext;

import siso.calendarselector.lib.calendarselectoractivity;

public class mainactivity extends activity {
 private edittext orderet;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
 }

 public void jump(view v) {
  edittext et = (edittext) findviewbyid(r.id.et_input);
  orderet = (edittext) findviewbyid(r.id.et_order);
  int days = integer.valueof(et.gettext().tostring());
  string order = orderet.gettext().tostring();
  intent i = new intent(mainactivity.this, calendarselectoractivity.class);
  i.putextra(calendarselectoractivity.days_of_select, days);
  i.putextra(calendarselectoractivity.order_day, order);
  startactivityforresult(i, 1);
 }

 @override
 protected void onactivityresult(int requestcode, int resultcode, intent data) {
  super.onactivityresult(requestcode, resultcode, data);
  if (requestcode == 1 && resultcode == result_ok) {
   string orderinfo = data.getstringextra(calendarselectoractivity.order_day);
   orderet.settext(orderinfo);
   /* *****注意*****
   // 如需转换为calendar
   // 正确转换方法(因为2月没有30天):
   string[] info = orderinfo.split("#");
   calendar c = calendar.getinstance();
   c.set(integer.valueof(info[0]), integer.valueof(info[1]) - 1, integer.valueof(info[2]));
   // 错误转换方法:
   c.set(integer.valueof(info[0]), integer.valueof(info[1]), integer.valueof(info[2]));
   c.add(calendar.month, -1);
   **/
  }
 }

}

calendaradapter.java代码:

package siso.calendarselector.lib;

import java.util.arraylist;
import java.util.calendar;

import android.content.context;
import android.graphics.color;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.textview;

import siso.calendarselector.r;


/**
 * @description 日历表格适配器
 */
public class calendaradapter extends baseadapter {

 private arraylist<day> days;
 private layoutinflater minflater;
 private calendar c;
 private context context;
 private string orderday;

 public calendaradapter(context context, calendar c, int passdays, string orderday) {
  this.c = c;
  this.context = context;
  this.orderday = orderday;
  days = calendarutils.getdaysofmonth(this.c, passdays, orderday);
  minflater = layoutinflater.from(context);
 }

 @override
 public int getcount() {
  return days.size();
 }

 @override
 public day getitem(int arg0) {
  return days.get(arg0);
 }

 @override
 public long getitemid(int arg0) {
  return 0;
 }

 @override
 public view getview(int arg0, view arg1, viewgroup arg2) {
  view v = arg1;
  viewholder holder = null;
  if (v == null) {
   v = minflater.inflate(r.layout.calendar_item, arg2, false);
   holder = new viewholder();
   holder.tv = (textview) v.findviewbyid(r.id.tv_calendar_item);
   v.settag(holder);
  } else {
   holder = (viewholder) v.gettag();
  }
  day d = getitem(arg0);
  switch (d.gettype()) {
  case today:
   setorderthreedaystyle(holder.tv, d.isordered(), context.getstring(r.string.today));
   break;
  case tomorrow:
   setorderthreedaystyle(holder.tv, d.isordered(), context.getstring(r.string.tomorrow));
   break;
  case t_d_a_t:
   setorderthreedaystyle(holder.tv, d.isordered(), context.getstring(r.string.t_d_a_t));
   break;
  case enable:
   holder.tv.settext(d.isordered() ? d.getname() + context.getstring(r.string.order_day) : d.getname());
   holder.tv.setenabled(true);
   holder.tv.settextcolor(d.isordered() ? color.white : context.getresources().getcolor(
     r.color.calendar_enable_color));
   holder.tv.setbackgroundresource(d.isordered() ? r.drawable.calendar_order_item_bg
     : r.drawable.normal_calendar_order_item_bg);
   holder.tv.settextsize(d.isordered() ? context.getresources().getdimension(
     r.dimen.calendar_item_order_day_size) : context.getresources().getdimension(
     r.dimen.calendar_item_nonorder_day_size));
   break;
  case not_enable:
   holder.tv.settext(d.getname());
   holder.tv.setenabled(false);
   holder.tv.settextcolor(context.getresources().getcolor(r.color.calendar_disable_color));
   holder.tv.setbackgroundcolor(color.white);
   holder.tv.settextsize(context.getresources().getdimension(r.dimen.calendar_item_nonorder_day_size));
   break;
  }

  return v;
 }

 private void setorderthreedaystyle(textview tv, boolean ordered, string daystr) {
  tv.settext(ordered ? daystr + context.getstring(r.string.order_day) : daystr);
  tv.setenabled(true);
  tv.settextcolor(ordered ? color.white : context.getresources().getcolor(r.color.calendar_threeday_color));
  tv.setbackgroundresource(ordered ? r.drawable.calendar_order_item_bg : r.drawable.normal_calendar_order_item_bg);
  tv.settextsize(ordered ? context.getresources().getdimension(r.dimen.calendar_item_order_day_size) : context
    .getresources().getdimension(r.dimen.calendar_item_nonorder_day_size));
 }

 static class viewholder {
  textview tv;
 }

 public void previous() {
  if (c.get(calendar.month) == c.getactualminimum(calendar.month)) {
   c.set((c.get(calendar.year) - 1), c.getactualmaximum(calendar.month), 1);
  } else {
   c.set(calendar.month, c.get(calendar.month) - 1);
  }
  days = calendarutils.getdaysofmonth(c, 0, orderday);
  notifydatasetchanged();
 }

 public void next() {
  if (c.get(calendar.month) == c.getactualmaximum(calendar.month)) {
   c.set((c.get(calendar.year) + 1), c.getactualminimum(calendar.month), 1);
  } else {
   c.set(calendar.month, c.get(calendar.month) + 1);
  }
  days = calendarutils.getdaysofmonth(c, 0, orderday);
  notifydatasetchanged();
 }

}

calendarlistadapter.java代码:

package siso.calendarselector.lib;

import java.util.calendar;

import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.adapterview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.baseadapter;
import android.widget.textview;

import siso.calendarselector.r;


/**
 * @description 日历列表适配器
 */
public class calendarlistadapter extends baseadapter {

 private context context;
 private layoutinflater minflater;
 private int daysofselect;
 private string orderday;
 private oncalendarorderlistener listener;

 public calendarlistadapter(context context, int daysofselect, string orderday) {
  this.context = context;
  this.daysofselect = daysofselect;
  this.orderday = orderday;
  minflater = layoutinflater.from(context);
 }

 @override
 public int getcount() {
  return calendarutils.throughmonth(calendar.getinstance(), daysofselect) + 1;
 }

 @override
 public object getitem(int arg0) {
  return null;
 }

 @override
 public long getitemid(int arg0) {
  return 0;
 }

 @override
 public view getview(int arg0, view arg1, viewgroup arg2) {
  view v = arg1;
  viewholder holder = null;
  if (v == null) {
   v = minflater.inflate(r.layout.calendar, arg2, false);
   holder = new viewholder();
   holder.yearandmonth = (textview) v.findviewbyid(r.id.tv_year_month);
   holder.calendargrid = (noscrollgridview) v.findviewbyid(r.id.gv_calendar_layout);
   v.settag(holder);
  } else {
   holder = (viewholder) v.gettag();
  }
  final calendar c = calendar.getinstance();
  c.add(calendar.month, arg0);
  holder.yearandmonth.settext(c.get(calendar.year) + context.getstring(r.string.year)
    + (c.get(calendar.month) + 1) + context.getstring(r.string.month));

  calendaradapter cadapter = null;
  if (arg0 == 0) {
   cadapter = new calendaradapter(context, c, daysofselect, orderday);
  } else {
   int d = daysofselect - calendarutils.currentmonthremaindays() - calendarutils.getflowmonthdays(arg0 - 1);
   cadapter = new calendaradapter(context, c, d, orderday);
  }
  holder.calendargrid.setadapter(cadapter);

  holder.calendargrid.setonitemclicklistener(new onitemclicklistener() {

   @override
   public void onitemclick(adapterview<?> parent, view view, int position, long id) {
    calendar cl = (calendar) c.clone();
    cl.set(calendar.day_of_month, 1);
    int day = position + 2 - cl.get(calendar.day_of_week);
    textview daytv = (textview) view.findviewbyid(r.id.tv_calendar_item);
    if (day <= 0 || !daytv.isenabled())
     return;
    string orderinfo = c.get(calendar.year) + "#" + (c.get(calendar.month) + 1) + "#" + day;
    cl.clear();
    cl = null;
    if (listener != null)
     listener.onorder(orderinfo);
   }
  });

  return v;
 }

 static class viewholder {
  textview yearandmonth;
  noscrollgridview calendargrid;
 }

 public void setoncalendarorderlistener(oncalendarorderlistener listener) {
  this.listener = listener;
 }

 public interface oncalendarorderlistener {
  void onorder(string orderinfo);
 }

}

calendarselectoractivity.java代码:

package siso.calendarselector.lib;

import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.widget.listview;

import siso.calendarselector.r;


/**
 * @description 预订日选择
 */
public class calendarselectoractivity extends activity {

 /**
  * 可选天数
  */
 public static final string days_of_select = "days_of_select";
 /**
  * 上次预订日
  */
 public static final string order_day = "order_day";

 private int daysofselect;
 private string orderday;

 private listview listview;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.calendar_selector);
  daysofselect = getintent().getintextra(days_of_select, 30);
  orderday = getintent().getstringextra(order_day);
  listview = (listview) findviewbyid(r.id.lv_calendar);

  calendarlistadapter adapter = new calendarlistadapter(this, daysofselect, orderday);
  listview.setadapter(adapter);

  adapter.setoncalendarorderlistener(new calendarlistadapter.oncalendarorderlistener() {

   @override
   public void onorder(string orderinfo) {
    intent result = new intent();
    result.putextra(order_day, orderinfo);
    setresult(result_ok, result);
    finish();
   }
  });
 }
}

calendarutils.java代码:

package siso.calendarselector.lib;

import java.util.arraylist;
import java.util.calendar;



public class calendarutils {

 private static int dayofmonth, monthofyear, curyear;

 static {
  dayofmonth = calendar.getinstance().get(calendar.day_of_month);
  monthofyear = calendar.getinstance().get(calendar.month);
  curyear = calendar.getinstance().get(calendar.year);
 }

 /**
  * gets the days in month.
  *
  * @param month the month
  * @param year the year
  * @return the days in month
  */
 public static int getdaysinmonth(int month, int year) {
  switch (month) {
  case calendar.january:
  case calendar.march:
  case calendar.may:
  case calendar.july:
  case calendar.august:
  case calendar.october:
  case calendar.december:
   return 31;
  case calendar.april:
  case calendar.june:
  case calendar.september:
  case calendar.november:
   return 30;
  case calendar.february:
   return (year % 4 == 0) ? 29 : 28;
  default:
   throw new illegalargumentexception("invalid month");
  }
 }

 /**
  * gets the flow month days.
  *
  * @param flowmonth the flow month
  * @return the flow month days
  */
 public static int getflowmonthdays(int flowmonth) {
  int totaldays = 0;
  for (int i = 0; i < flowmonth; i++) {
   calendar c = calendar.getinstance();
   c.add(calendar.month, i + 1);
   int days = getdaysinmonth(c.get(calendar.month), c.get(calendar.year));
   totaldays += days;
  }
  return totaldays;
 }

 /**
  * current month remain days.
  *
  * @return the int
  */
 public static int currentmonthremaindays() {
  calendar c = calendar.getinstance();
  return getdaysinmonth(c.get(calendar.month), c.get(calendar.year)) - c.get(calendar.day_of_month) + 1;
 }

 /**
  * through month.
  *
  * @param calendar the calendar
  * @param passdays the pass days
  * @return the int
  */
 public static int throughmonth(calendar calendar, int passdays) {
  calendar c = (calendar) calendar.clone();
  int curmonth = c.get(calendar.month);
  int curyear = c.get(calendar.year);
  c.add(calendar.day_of_month, passdays - 1);
  int monthcount = (c.get(calendar.year) - curyear) * 12 + (c.get(calendar.month) - curmonth);
  return monthcount;
 }

 /**
  * gets the days of month.
  *
  * @param calendar the calendar
  * @return the days of month
  */
 public static string[] getdaysofmonth(calendar calendar) {
  calendar month = (calendar) calendar.clone();
  string[] days;
  final int first_day_of_week = 0; // sunday = 0, monday = 1
  month.set(calendar.day_of_month, 1);
  int lastday = month.getactualmaximum(calendar.day_of_month);
  int firstday = (int) month.get(calendar.day_of_week);

  if (firstday == 1) {
   days = new string[lastday + (first_day_of_week * 6)];
  } else {
   days = new string[lastday + firstday - (first_day_of_week + 1)];
  }

  int j = first_day_of_week;

  if (firstday > 1) {
   for (j = 0; j < firstday - first_day_of_week; j++) {
    days[j] = "";
   }
  } else {
   for (j = 0; j < first_day_of_week * 6; j++) {
    days[j] = "";
   }
   j = first_day_of_week * 6 + 1; // sunday => 1, monday => 7
  }

  int daynumber = 1;
  for (int i = j - 1; i < days.length; i++) {
   days[i] = "" + daynumber;
   daynumber++;
  }
  return days;
 }

 /**
  * gets the days of month.
  *
  * @param calendar the calendar
  * @param passdays the pass days
  * @param orderday the order day
  * @return the days of month
  */
 public static arraylist<day> getdaysofmonth(calendar calendar, int passdays, string orderday) {
  string[] orderinfo = null;
  boolean isordered = false;
  if (orderday != null)
   orderinfo = orderday.split("#");
  calendar month = (calendar) calendar.clone();
  final int first_day_of_week = 0; // sunday = 0, monday = 1
  month.set(calendar.day_of_month, 1);
  int lastday = month.getactualmaximum(calendar.day_of_month);
  int firstday = (int) month.get(calendar.day_of_week);

  arraylist<day> days = new arraylist<day>();
  int size;
  if (firstday == 1) {
   size = lastday + (first_day_of_week * 6);
  } else {
   size = lastday + firstday - (first_day_of_week + 1);
  }

  for (int i = 0; i < size; i++) {
   days.add(new day("", day.daytype.not_enable, isordered));
  }

  int j = first_day_of_week;

  if (firstday > 1) {
   for (j = 0; j < firstday - first_day_of_week; j++) {
    days.set(j, new day("", day.daytype.not_enable, isordered));
   }
  } else {
   for (j = 0; j < first_day_of_week * 6; j++) {
    days.set(j, new day("", day.daytype.not_enable, isordered));
   }
   j = first_day_of_week * 6 + 1; // sunday => 1, monday => 7
  }

  int daynumber = 1;

  for (int i = j - 1; i < days.size(); i++) {
   day.daytype type;
   if (month.get(calendar.year) == curyear && month.get(calendar.month) == monthofyear) {
    if (daynumber >= dayofmonth && daynumber < dayofmonth + passdays) {
     type = day.daytype.enable;
     if (daynumber == dayofmonth) {
      type = day.daytype.today;
      if (orderday == null) {
       isordered = true;
      } else {
       isordered = false;
      }
     } else if (daynumber == dayofmonth + 1) {
      type = day.daytype.tomorrow;
     } else if (daynumber == dayofmonth + 2) {
      type = day.daytype.t_d_a_t;
     }
    } else {
     type = day.daytype.not_enable;
    }
   } else {
    if (daynumber <= passdays) {
     type = day.daytype.enable;
     // 明天/后天在下个月
     int remaindays = getdaysinmonth(monthofyear, curyear) - dayofmonth;
     if (remaindays < 2 && daynumber <= 2 && math.abs(month.get(calendar.month) - monthofyear) == 1
       && month.get(calendar.year) == curyear) {
      if (remaindays == 1) {
       if (daynumber == 1) {
        type = day.daytype.t_d_a_t;
       }
      } else if (remaindays == 0) {
       if (daynumber == 1) {
        type = day.daytype.tomorrow;
       } else if (daynumber == 2) {
        type = day.daytype.t_d_a_t;
       }
      }
     }
    } else {
     type = day.daytype.not_enable;
    }
   }
   if (orderinfo != null && orderinfo.length == 3 && integer.valueof(orderinfo[0]) == month.get(calendar.year)
     && integer.valueof(orderinfo[1]) == (month.get(calendar.month) + 1)
     && integer.valueof(orderinfo[2]) == daynumber) {
    isordered = true;
   } else {
    isordered = false;
   }

   days.set(i, new day("" + daynumber, type, isordered));
   daynumber++;
  }
  return days;
 }
}

day.java代码:

package siso.calendarselector.lib;

public class day {

public day(string name, daytype type, boolean isordered) {
 setname(name);
 settype(type);
 setordered(isordered);
}

public enum daytype {
 today, tomorrow, t_d_a_t, enable, not_enable
}

private string name;
private daytype type;
private boolean isordered;

public boolean isordered() {
 return isordered;
}

public void setordered(boolean isordered) {
 this.isordered = isordered;
}

public string getname() {
 return name;
}

public void setname(string name) {
 this.name = name;
}

public daytype gettype() {
 return type;
}

public void settype(daytype type) {
 this.type = type;
}
}

noscrollgridview.java代码:

package siso.calendarselector.lib;

import android.content.context;
import android.util.attributeset;
import android.widget.gridview;

public class noscrollgridview extends gridview {

 public noscrollgridview(context context) {
  super(context);

 }

 public noscrollgridview(context context, attributeset attrs) {
  super(context, attrs);
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  int expandspec = measurespec.makemeasurespec(integer.max_value >> 2, measurespec.at_most);
  super.onmeasure(widthmeasurespec, expandspec);
 }

}

androidmanifest.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ndroid="http://schemas.android.com/apk/res-auto"
 package="siso.calendarselector">

 <application
  android:allowbackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:supportsrtl="true"
  ndroid:theme="@android:style/theme.black.notitlebar">
  <activity android:name=".mainactivity">
   <intent-filter>
    <action android:name="android.intent.action.main" />

    <category android:name="android.intent.category.launcher" />
   </intent-filter>
  </activity>
  <activity android:name="siso.calendarselector.lib.calendarselectoractivity">

  </activity>
 </application>

</manifest>

Android仿淘宝预订日历(18)

项目运行结果如图:

Android仿淘宝预订日历(18)

Android仿淘宝预订日历(18)

Android仿淘宝预订日历(18)

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

上一篇:

下一篇: