开门见山,点击签到按钮直接签到,弹出dialog,先上效果图
demo是利用gridview实现的,现附上布局文件
效果图:
dialog中的主要代码:
public class SignDataDialog extends Dialog implements View.OnClickListener{
private GridView gridView;
private ImageView iv_front,iv_next;
private TextView tv_date,tv_sign_days,tv_jifen;
private MyCalendarAdapter adapter;
private SpecialCalendar sp;
private Context context;
private SignDateModle modle;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
private String systime;
private int year,month;
String[] b=new String[]{
"0","0","0","0","0","0","0",
"0","0","0","0","0","0","0",
"0","0","0","0","0","0","0",
"0","0","0","0","0","0","0",
"0","0","0","0","0","0","0",
"0","0","0","0","0","0","0"
};
public SignDataDialog(Context context,SignDateModle modle) {
super(context);
this.context=context;
this.modle=modle;
setContentView(R.layout.dialog_sign_data);
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.CENTER;
window.setBackgroundDrawableResource(android.R.color.transparent);
window.setAttributes(params);
setCanceledOnTouchOutside(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp=new SpecialCalendar();
Date date = new Date();
systime = sdf.format(date);
year=Integer.valueOf(systime.split("-")[0]);
month=Integer.valueOf(systime.split("-")[1]);
iv_front= (ImageView) findViewById(R.id.iv_front);
iv_next= (ImageView) findViewById(R.id.iv_next);
gridView= (GridView) findViewById(R.id.gv_sign_date);
tv_date= (TextView) findViewById(R.id.tv_date);
tv_sign_days= (TextView) findViewById(R.id.tv_sign_num);
tv_jifen= (TextView) findViewById(R.id.tv_jifen_num);
tv_date.setText(systime);
iv_front.setOnClickListener(this);
iv_next.setOnClickListener(this);
if (modle!=null) {
String dates=modle.getDateTime();
adapter = new MyCalendarAdapter(context, Integer.valueOf(dates.split("-")[0]), Integer.valueOf(dates.split("-")[1]), 17, getStrings(modle));
gridView.setAdapter(adapter);
}
}
public String[] getStrings(SignDateModle modle){
int year=Integer.valueOf(modle.getDateTime().split("-")[0]);
int moth=Integer.valueOf(modle.getDateTime().split("-")[1]);
int start=sp.getWeekdayOfMonth(year,moth);
for (int i=0;i
其计算日期的主要代码在适配器中:
class MyCalendarAdapter extends BaseAdapter{
private boolean isLeapyear = false; // 是否为闰年
private int daysOfMonth = 0; // 某月的天数
private int dayOfWeek = 0; // 具体某一天是星期几
private int lastDaysOfMonth = 0; // 上一个月的总天数
private Context context;
private String[] dayNumber = new String[42]; // 一个gridview中的日期存入此数组中
private SpecialCalendar sc = null;
private String currentYear = "";
private String currentMonth = "";
private int currentFlag = -1; // 用于标记当天
private String showYear = ""; // 用于在头部显示的年份
private String showMonth = ""; // 用于在头部显示的月份
private String animalsYear = "";
private String leapMonth = ""; // 闰哪一个月
// 系统当前时间
private String sysDate = "";
private String sys_year = "";
private String sys_month = "";
private String sys_day = "";
private boolean flag;// 标记是不是本月
private String[] data;
private int a;
public MyCalendarAdapter() {
Date date = new Date();
sysDate = sdf.format(date); // 当期日期
sys_year = sysDate.split("-")[0];
sys_month = sysDate.split("-")[1];
sys_day = sysDate.split("-")[2];
}
public MyCalendarAdapter(Context context,int year_c,int month_c, int day_c, String[] a) {
// TODO Auto-generated constructor stub
this();
this.context = context;
sc = new SpecialCalendar();
currentYear = String.valueOf(year_c);// 得到当前的年份
currentMonth = String.valueOf(month_c);
getCalendar(Integer.parseInt(currentYear),
Integer.parseInt(currentMonth));
data = a;
}
@Override
public int getCount() {
return dayNumber.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null){
holder=new ViewHolder();
convertView= LayoutInflater.from(context).inflate(R.layout.item_sign_data,null);
holder.iv_bg= (ImageView) convertView.findViewById(R.id.iv_bg);
holder.tv_date= (TextView) convertView.findViewById(R.id.tv_date);
convertView.setTag(holder);
}else {
holder= (ViewHolder) convertView.getTag();
}
String d = dayNumber[position];
holder.tv_date.setText(d);
holder.tv_date.setTextColor(Color.GRAY);
if (position < daysOfMonth + dayOfWeek && position >= dayOfWeek) {
// 当前月信息显示
holder.tv_date.setTextColor(Color.parseColor("#2a2a2a"));// 当月字体设黑
flag = true;
a++;
} else {
flag = false;
}
if (flag) {
if (a <= data.length) {
String att = data[a - 1];
if (att.equals("1")) {//签到
holder.tv_date.setTextColor(Color.parseColor("#2a2a2a"));
holder.iv_bg.setVisibility(View.VISIBLE);
}
}
}
if (currentFlag == position) {
// 设置当天的背景
// String dv="今日";
// SpannableString sp = new SpannableString(d + "\n" + dv);//当天字体加粗
// sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
// d.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// sp.setSpan(new RelativeSizeSpan(1.2f), 0, d.length(),
// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// sp.setSpan(new RelativeSizeSpan(0.75f), d.length() ,
// dayNumber[position].length()+ 3,
// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// holder.tv_date.setText(sp);
// holder.iv_bg.setVisibility(View.VISIBLE);
holder.iv_bg.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_sign_bg_today));
}
return convertView;
}
class ViewHolder{
private ImageView iv_bg;
private TextView tv_date;
}
// 得到某年的某月的天数且这月的第一天是星期几
public void getCalendar(int year, int month) {
isLeapyear = sc.isLeapYear(year); // 是否为闰年
daysOfMonth = sc.getDaysOfMonth(isLeapyear, month); // 某月的总天数
dayOfWeek = sc.getWeekdayOfMonth(year, month); // 某月第一天为星期几
lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1); // 上一个月的总天数
getweek(year, month);
}
// 将一个月中的每一天的值添加入数组dayNuber中
private void getweek(int year, int month) {
int j = 1;
int flag = 0;
String lunarDay = "";
// 得到当前月的所有日程日期(这些日期需要标记)
for (int i = 0; i < dayNumber.length; i++) {
if (i < dayOfWeek) { // 前一个月
int temp = lastDaysOfMonth - dayOfWeek + 1;
dayNumber[i] = (temp + i) + "";
} else if (i < daysOfMonth + dayOfWeek) { // 本月
String day = String.valueOf(i - dayOfWeek + 1); // 得到的日期
dayNumber[i] = i - dayOfWeek + 1 + "";
// 对于当前月才去标记当前日期
if (sys_year.equals(String.valueOf(year))
&& sys_month.equals(String.valueOf(month))
&& sys_day.equals(day)) {
// 标记当前日期
currentFlag = i;
}
setShowYear(String.valueOf(year));
setShowMonth(String.valueOf(month));
} else { // 下一个月
dayNumber[i] = j + "";
j++;
}
}
String abc = "";
for (int i = 0; i < dayNumber.length; i++) {
abc = abc + dayNumber[i] + ":";
}
}
public String getShowYear() {
return showYear;
}
public void setShowYear(String showYear) {
this.showYear = showYear;
}
public String getShowMonth() {
return showMonth;
}
public void setShowMonth(String showMonth) {
this.showMonth = showMonth;
}
public String getAnimalsYear() {
return animalsYear;
}
public void setAnimalsYear(String animalsYear) {
this.animalsYear = animalsYear;
}
public String getLeapMonth() {
return leapMonth;
}
public void setLeapMonth(String leapMonth) {
this.leapMonth = leapMonth;
}
}
public class SpecialCalendar {
private int daysOfMonth = 0; // 某月的天数
private int dayOfWeek = 0; // 具体某一天是星期几
// 判断是否为闰年
public boolean isLeapYear(int year) {
if (year % 100 == 0 && year % 400 == 0) {
return true;
} else if (year % 100 != 0 && year % 4 == 0) {
return true;
}
return false;
}
// 得到某月有多少天数
public int getDaysOfMonth(boolean isLeapyear, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysOfMonth = 31;
break;
case 4:
case 6:
case 9:
case 11:
daysOfMonth = 30;
break;
case 2:
if (isLeapyear) {
daysOfMonth = 29;
} else {
daysOfMonth = 28;
}
}
return daysOfMonth;
}
// 指定某年中的某月的第一天是星期几
public int getWeekdayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, 1);
dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
return dayOfWeek;
}
}
积分当然是交给后台处理返回了,ok,that's all!!!明天又要上班了....