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

自定义日历

2016-06-27 00:00 567 查看
摘要: 一个自定义日历 可左右滑动 可添加点击事件 日期圆点背景

源码地址 https://github.com/Reone/CalendarView

http://blog.csdn.net/wwj_748/article/details/42244865 基础上进行修改,感谢

效果图



自定义view



Activity 中引用

package com.reone.calendarview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.reone.calendarview.view.CalendarCard;
import com.reone.calendarview.view.CalendarPickView;
import com.reone.calendarview.view.CustomDate;

public class MainCalendarActivity extends Activity implements View.OnClickListener{

private CalendarPickView mCalendarPickView;
private Context mContext;

private ImageButton preImgBtn, nextImgBtn;
private TextView monthText;
private ImageButton closeImgBtn;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main_calendar);
mContext =this;
mCalendarPickView = (CalendarPickView) findViewById(R.id.calendar_pick_view);
mViewPager = mCalendarPickView.getViewPager();
preImgBtn = (ImageButton) this.findViewById(R.id.btnPreMonth);
nextImgBtn = (ImageButton) this.findViewById(R.id.btnNextMonth);
monthText = (TextView) this.findViewById(R.id.tvCurrentMonth);
closeImgBtn = (ImageButton) this.findViewById(R.id.btnClose);
preImgBtn.setOnClickListener(this);
nextImgBtn.setOnClickListener(this);
closeImgBtn.setOnClickListener(this);
monthText.setText(mCalendarPickView.getCurrentDate().month+"月");

mCalendarPickView.setOnItemClickListener(new CalendarPickView.OnItemClickListener() {
@Override
public void onClick(CustomDate date, CalendarCard.State state) {
Toast.makeText(mContext,"onClick " + date.toString(), Toast.LENGTH_SHORT).show();
}

@Override
public void onChangeDate(CustomDate date) {
monthText.setText(date.month + "月");
}
});

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnPreMonth:
mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
break;
case R.id.btnNextMonth:
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
break;
case R.id.btnClose:
finish();
break;
default:
break;
}
}

}

activity_main_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#f6f1ea"
>

<ImageButton
android:id="@+id/btnPreMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="33dip"
android:layout_toLeftOf="@+id/tvCurrentMonth"
android:background="@android:drawable/ic_media_previous" />

<ImageButton
android:id="@+id/btnNextMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="33dip"
android:layout_toRightOf="@+id/tvCurrentMonth"
android:background="@android:drawable/ic_media_next" />

<TextView
android:id="@+id/tvCurrentMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="11月"
android:textColor="#323232"
android:textSize="22sp" />

<ImageButton
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="@android:drawable/ic_menu_close_clear_cancel" />
</RelativeLayout>

<com.reone.calendarview.view.CalendarPickView
android:id="@+id/calendar_pick_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>

</LinearLayout>

本博客原地址:http://my.oschina.net/reone/blog/701483
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息