|
|
@@ -19,14 +19,15 @@ import java.util.Set;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
+ * update 2018-1-4 更新添加可以选择过去的时间过去时间不可点击,变灰等
|
|
|
+ * setCanClickHistory//是否需要向过去的时间变灰,当设置为ture时候默认不可以点击,默认值为flase
|
|
|
+ * setNeedHintHistory//是否可以点击过去的日期,默认为true
|
|
|
* Created by pengminggong on 2016/9/20.
|
|
|
*/
|
|
|
public class OACalendarView extends View implements View.OnTouchListener {
|
|
|
//日期记录
|
|
|
private Date curDate; // 当前日历显示的月
|
|
|
- private Date today; // 今天的日期
|
|
|
private Date downDate; // 手指按下状态时临时日期
|
|
|
//索引
|
|
|
private int todayIndex; // 今天的索引
|
|
|
@@ -50,7 +51,7 @@ public class OACalendarView extends View implements View.OnTouchListener {
|
|
|
public OACalendarView(Context context, AttributeSet attrs) {
|
|
|
super(context, attrs);
|
|
|
surface = new Surface();
|
|
|
- curDate = today = new Date();
|
|
|
+ curDate = new Date();
|
|
|
calendar = Calendar.getInstance();
|
|
|
calendar.setTime(curDate);
|
|
|
todayIndex = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
@@ -72,9 +73,20 @@ public class OACalendarView extends View implements View.OnTouchListener {
|
|
|
setOnTouchListener(this);
|
|
|
}
|
|
|
|
|
|
+ private boolean needHintHistory=false;
|
|
|
+ private boolean canClickHistory=true;
|
|
|
+
|
|
|
+ public void setCanClickHistory(boolean canClickHistory) {
|
|
|
+ this.canClickHistory = canClickHistory;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setNeedHintHistory(boolean needHintHistory) {
|
|
|
+ this.needHintHistory = needHintHistory;
|
|
|
+ this.canClickHistory = !needHintHistory;
|
|
|
+ }
|
|
|
|
|
|
private void setCalendar(Date d) {
|
|
|
- curDate = today = d;
|
|
|
+ curDate =d;
|
|
|
calendar.setTime(curDate);
|
|
|
calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为当月第一天
|
|
|
startIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;//获取第一天在当周的星期几,返回多1,所以减1
|
|
|
@@ -188,7 +200,7 @@ public class OACalendarView extends View implements View.OnTouchListener {
|
|
|
|
|
|
//绘画点击的内容
|
|
|
private void drawSelect(Canvas canvas) {
|
|
|
- if (downIndex >= startIndex && downIndex <= endIndex) {
|
|
|
+ if (downIndex >= startIndex && downIndex <= endIndex && canClickHistory) {
|
|
|
//圆的最中心
|
|
|
float cellY = surface.weekHeight + surface.cellHeight * (downIndex / 7) + surface.cellHeight / 2;
|
|
|
float cellX = surface.cellWidth * (downIndex % 7) + surface.cellWidth / 2;
|
|
|
@@ -229,6 +241,8 @@ public class OACalendarView extends View implements View.OnTouchListener {
|
|
|
}
|
|
|
if (downIndex == i) {
|
|
|
surface.datePaint.setColor(surface.selectColor);
|
|
|
+ } else if (isHistory(i)) {
|
|
|
+ surface.datePaint.setColor(surface.hineDateColor);
|
|
|
}
|
|
|
float dayTextX = surface.cellWidth * (i % surface.weekText.length) +
|
|
|
(surface.cellWidth - surface.datePaint.measureText(chche)) / 2f;
|
|
|
@@ -270,6 +284,22 @@ public class OACalendarView extends View implements View.OnTouchListener {
|
|
|
return false;
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ public boolean isHistory(int index) {
|
|
|
+ if (needHintHistory) {
|
|
|
+ calendar.setTime(curDate);
|
|
|
+ int month = calendar.get(Calendar.MONTH);
|
|
|
+ int year = calendar.get(Calendar.YEAR);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, date[index]);
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ if (year == c.get(Calendar.YEAR) && month == c.get(Calendar.MONTH) && todayIndex == index) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return calendar.compareTo(c) < 0;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
/*end 判断*/
|
|
|
|
|
|
/**
|
|
|
@@ -302,7 +332,9 @@ public class OACalendarView extends View implements View.OnTouchListener {
|
|
|
int indexX = (int) Math.floor(x / surface.cellWidth);
|
|
|
int indexY = (int) Math.floor((y - surface.weekHeight) / surface.cellHeight);
|
|
|
downIndex = indexY * 7 + indexX;
|
|
|
- if (downIndex < startIndex || downIndex > endIndex) return false;//当点击的是非本月的日期时候
|
|
|
+ if (downIndex < startIndex || downIndex > endIndex || !canClickHistory)
|
|
|
+ return false;//当点击的是非本月的日期时候
|
|
|
+
|
|
|
calendar.setTime(curDate);
|
|
|
calendar.set(Calendar.DAY_OF_MONTH, date[downIndex]);
|
|
|
downDate = calendar.getTime();
|
|
|
@@ -328,10 +360,10 @@ public class OACalendarView extends View implements View.OnTouchListener {
|
|
|
|
|
|
/*设置月份*/
|
|
|
public void setCurDate(Date date) {
|
|
|
- LogUtil.d("Test",""+ DateFormatUtil.getStrDate4Date(date,"yyyy-MM-dd"));
|
|
|
+ LogUtil.d("Test", "" + DateFormatUtil.getStrDate4Date(date, "yyyy-MM-dd"));
|
|
|
curDate = date;
|
|
|
calendar.setTime(curDate);
|
|
|
- int curMoth=calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
+ int curMoth = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
startIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;//获取第一天在当周的星期几,返回多1,所以减1
|
|
|
maxDay = calendar.getActualMaximum(Calendar.DATE);//获取当月有多少天
|
|
|
@@ -341,11 +373,11 @@ public class OACalendarView extends View implements View.OnTouchListener {
|
|
|
for (int i = startIndex, j = 1; i < this.date.length; i++, j++) {
|
|
|
this.date[i] = j;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// if (isThisMonth()) {
|
|
|
// downIndex = todayIndex;
|
|
|
// } else {
|
|
|
- this.downIndex = Math.min(startIndex + curMoth - 1, this.date.length - 1);
|
|
|
+ this.downIndex = Math.min(startIndex + curMoth - 1, this.date.length - 1);
|
|
|
// }
|
|
|
surface.init();
|
|
|
invalidate();
|