您的位置:首页 > 其它

自定义的一个日历Calender

2015-08-21 11:50 330 查看
产品要做签到功能,签到功能要基于一个日历来进行,所以就根据 要求自定义了一个日历



自定义控件相信做android都知道:

(1)首先创建一个类,继承一个容器类或者是一个控件

(2)然后就是你需要设置的属性等的,在attrs文件夹中

(3)然后就是在类里边进行属性的设置以及布局等等功能的添加

其实自定义一个日历问题都不多,很多人都会想到通过一个gridView然后填充就可以,确实是这样,主要是在显示每个月的第一天的位置以及每个月显示多少天有点绕。

思路:通过判断当前星期几然后进行日历的填充,但是填充的大小不能大于当月天数,通过当月第一天是星期几来判断从哪个位置填充。

代码:

package com.example.calenderdemo;

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;

public class CalendarViewAdapter extends BaseAdapter {

	private Context mContext;
	private int mCountDay = 49;
	private int mCurrent_mouth_Countday;
	private int mCurrent_Week;
	private int id[] = { R.string.week7, R.string.week1, R.string.week2,
			R.string.week3, R.string.week4, R.string.week5, R.string.week6 };

	public CalendarViewAdapter(Context context, int countday) {
		this.mContext = context;
		this.mCurrent_Week = Utils.getCurrentMonthStart();
		this.mCurrent_mouth_Countday = countday;
	}

	@Override
	public int getCount() {
		return mCountDay;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_calendar, null);
			holder.mTv_calendar_day = (TextView) convertView
					.findViewById(R.id.tv_calendar_day);
			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();
		if (position <= 6) {
			holder.mTv_calendar_day.setTextColor(Color.BLACK);
			holder.mTv_calendar_day.setTextSize(mContext.getResources()
					.getDimension(R.dimen.text_size_7));
			holder.mTv_calendar_day.setText(mContext.getResources().getString(
					id[position]));
		} else {
			if (mCurrent_Week == 7 && (position -6) <= mCurrent_mouth_Countday) {
				holder.mTv_calendar_day.setText(position-6 + "");
			} else if (position -7>= mCurrent_Week
					&& position - mCurrent_Week -6 <= mCurrent_mouth_Countday) {
				holder.mTv_calendar_day.setText(position - mCurrent_Week -6
						+ "");
			}
		
		}
		if (position % 7 == 6) {
			holder.mTv_calendar_day.setBackgroundResource(R.drawable.line_right);
		}else if (position % 7 == 0) {
			holder.mTv_calendar_day.setBackgroundResource(R.drawable.line_left);
		}
		return convertView;
	}

	class ViewHolder {
		TextView mTv_calendar_day;
	}
}


把gridView填充了以后一个简单的日历控件就ok了。

Demo:http://download.csdn.net/detail/u012808234/9031525

自定义了一个日历以后就要做签到了,签到只要在自定义的Calendar中稍稍修改下就好了,看下效果图:



看这还行,然后DEMO: http://download.csdn.net/detail/u012808234/9040009
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: