您的位置:首页 > 移动开发 > Android开发

android自定义日历的使用方法

2015-05-07 20:39 344 查看
子夜枯灯123提供了一个自定义的android日历,他的博客地址如下:
http://blog.csdn.net/huangyanbin123/article/details/38350213
他提供的代码除了自定义日历外,还有一个demo,对于大部分人来说,仅仅需要展示一个日历,并响应日历的click事件即可。因此我简化了他的demo,给出了一个更加简单的demo和使用方法,方法分为三步:

第一步:导入
http://download.csdn.net/detail/huangyanbin123/7723323
中的代码。因为这不是一个library项目,因此直接将源代码加入你的工程即可。

第二步:建立一个如下的layout文件,主要是android.support.v4.view.ViewPager要放在合适的位置,它是日历控件的容器。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:textcircle="http://schemas.android.com/apk/res/com.example.calendar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#fffffe"
        android:orientation="vertical"
        android:paddingLeft="10dip"
        android:paddingRight="10dip" >

        <View
            android:layout_width="fill_parent"
            android:layout_height="1px"
            android:layout_alignParentTop="true"
            android:background="#20000000" >
        </View>

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="20dip"
            android:layout_marginBottom="2dip"
            android:layout_marginTop="2dip" >

            <TableRow>

                <TextView
                    style="@style/dateStyle"
                    android:text="日"
                    android:textColor="@color/date_1" />

                <TextView
                    style="@style/dateStyle"
                    android:text="一"
                    android:textColor="@color/date_2" />

                <TextView
                    style="@style/dateStyle"
                    android:text="二"
                    android:textColor="@color/date_2" />

                <TextView
                    style="@style/dateStyle"
                    android:text="三"
                    android:textColor="@color/date_2" />

                <TextView
                    style="@style/dateStyle"
                    android:text="四"
                    android:textColor="@color/date_2" />

                <TextView
                    style="@style/dateStyle"
                    android:text="五"
                    android:textColor="@color/date_2" />

                <TextView
                    style="@style/dateStyle"
                    android:text="六"
                    android:textColor="@color/date_1" />
            </TableRow>
        </TableLayout>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1px"
            android:layout_alignParentTop="true"
            android:background="#20000000" >
        </View>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/contentPager2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#fffffe" >

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#F7F7F7"
            android:paddingLeft="15dip"
            android:paddingRight="10dip" >
        </android.support.v4.view.ViewPager>
    </RelativeLayout>

</LinearLayout>


第三步:创建自己的Activity类,如下所示:

package com.kidapk.www;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.example.calendar.R;
import com.example.calendar.doim.CalendarViewBuilder;
import com.example.calendar.doim.CustomDate;
import com.example.calendar.widget.CalendarView;
import com.example.calendar.widget.CalendarView.CallBack;
import com.example.calendar.widget.CalendarViewPagerLisenter;
import com.example.calendar.widget.CustomViewPagerAdapter;

public class TestCalendarActivity extends Activity implements CallBack{
	
	private ViewPager viewPager;
	private CalendarView[] views;
	private CalendarViewBuilder builder = new CalendarViewBuilder();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		
		//初始化控件
		viewPager = (ViewPager) this.findViewById(R.id.viewpager2);
		views = builder.createMassCalendarViews(this, 5, this);   //产生多个CalendarView
		this.findViewById(R.id.contentPager2);
		
		//设置控件
		CustomViewPagerAdapter<CalendarView> viewPagerAdapter = new CustomViewPagerAdapter<CalendarView>(views);
		viewPager.setAdapter(viewPagerAdapter);
		viewPager.setCurrentItem(498);   //views可翻动498页
		viewPager.setOnPageChangeListener(new CalendarViewPagerLisenter(viewPagerAdapter));
		}
	
	@Override
	public void clickDate(CustomDate date) {
		Toast.makeText(this, date.year+"-"+date.month+"-"+date.day, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onMesureCellHeight(int cellSpace) {
		
	}

	@Override
	public void changeDate(CustomDate date) {
	}

}
app的效果如下图所示:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: