Android仿淘宝预订日历(18)

Android仿淘宝预订日历(18),第1张

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

本文实例为大家分享了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,context.getString(R.string.tomorrow));   break;  case T_D_A_T:   setorderThreeDayStyle(holder.tv,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,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),c.get(Calendar.MONTH) + 1);  }  days = CalendarUtils.getDaysOfMonth(c,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,VIEwGroup arg2) {  VIEw v = arg1;  VIEwHolder holder = null;  if (v == null) {   v = mInflater.inflate(R.layout.calendar,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,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,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,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,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("",isOrdered));   }  } else {   for (j = 0; j < FirsT_DAY_OF_WEEK * 6; j++) {    days.set(j,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)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147091.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存