Android仿淘宝预订日历(18)

本文实例为大家分享了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>

项目运行结果如图:

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

(0)

相关推荐

  • Android实现自定义日历

    自定义日历控件,支持旧历.节气.日期标注.点击操作 (参考网络上的日历控件改写) 注:将下面的四张资源图片拷贝到所建包的下一个image目录中,如Calendar.java 所在包为 cc.util.android.view,则需要再创建一个包cc.util.android.view.image 然后将图片拷贝进去 /****************从此出开始将代码拷贝到一个文件中*******************/ package cc.util.android.view; import

  • Android使用GridView实现日历的简单功能

    简单的日历实现,只是显示了每一个月,没有显示当天和记事这些功能 主要是计算月初是周几,月末是周几,然后相应的显示上一月多少天和下一月多少天. 先看一下关于日期的用到的几个工具类 /** * 获取该月天数 */ public static int getCurrentMonthDay(long millSec) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(millSec); calendar.set(

  • Android实现日历控件示例代码

    做的是一个酒店的项目,可以选择入住和离开的日期.声明为了省事在网上找的资料,自己修改的逻辑,希望对需要的朋友有帮助.喜欢的给个好评.谢谢啦!祝生活愉快! 先上图 第一步,搭建布局xml <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_w

  • java制作android 日历代码分享

    代码很简单,就不多废话了 复制代码 代码如下: //读取日历事件     public static void getCalendarInfo(Activity activity,String tag){         String[] projection = new String[]{CalendarContract.Events._ID,CalendarContract.Events.TITLE};         ContentResolver cr = activity.getCon

  • Android学习教程之日历库使用(15)

    本教程为大家分享了Android日历库的使用方法,供大家参考,具体内容如下 MainActivity.java代码: package siso.weekv; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCo

  • Android学习教程之日历控件使用(7)

    本文实例为大家分享了Android日历控件的使用方法,供大家参考,具体内容如下 MainActivity.java代码: package siso.timessquare; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; p

  • Android自定义日历Calender代码实现

    产品要做签到功能,签到功能要基于一个日历来进行,所以就根据 要求自定义了一个日历 自定义控件相信做android都知道: (1)首先创建一个类,继承一个容器类或者是一个控件 (2)然后就是你需要设置的属性等的,在attrs文件夹中 (3)然后就是在类里边进行属性的设置以及布局等等功能的添加 其实自定义一个日历问题都不多,很多人都会想到通过一个gridView然后填充就可以,确实是这样,主要是在显示每个月的第一天的位置以及每个月显示多少天有点绕. 思路:通过判断当前星期几然后进行日历的填充,但是填

  • Android自定义日历控件实例详解

    为什么要自定义控件 有时,原生控件不能满足我们对于外观和功能的需求,这时候可以自定义控件来定制外观或功能:有时,原生控件可以通过复杂的编码实现想要的功能,这时候可以自定义控件来提高代码的可复用性. 如何自定义控件 下面我通过我在github上开源的Android-CalendarView项目为例,来介绍一下自定义控件的方法.该项目中自定义的控件类名是CalendarView.这个自定义控件覆盖了一些自定义控件时常需要重写的一些方法. 构造函数 为了支持本控件既能使用xml布局文件声明,也可在ja

  • Android使用GridView实现日历功能示例(详细代码)

    Android使用GridView实现日历功能示例,代码有点多,发个图先: 如果懒得往下看的,可以直接下载源码吧,最近一直有人要,由于时间太久了,懒得找出来整理,今天又看到有人要,正好没事就整理了一下 源码下载.... 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

  • Android使用GridLayout绘制自定义日历控件

    效果图 思路:就是先设置Gridlayout的行列数,然后往里面放置一定数目的自定义日历按钮控件,最后实现日历逻辑就可以了. 步骤: 第一步:自定义日历控件(初步) 第二步:实现自定义单个日期按钮控件 第三步:将第二步得到的控件动态添加到第一步的布局中,并实现日期逻辑 第四步:编写单个日期点击监听器接口 第一步:自定义日历控件(初步) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln

随机推荐