欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android简单实现app每月签到功能

程序员文章站 2023-08-31 15:14:10
本文实例为大家分享了android实现app每月签到功能的具体代码,供大家参考,具体内容如下 先上一张效果图: 其中这些签到的效果图是在网上找的,然后重要用到的控件...

本文实例为大家分享了android实现app每月签到功能的具体代码,供大家参考,具体内容如下

先上一张效果图:

Android简单实现app每月签到功能

其中这些签到的效果图是在网上找的,然后重要用到的控件就是 gridvvew 了, 代码很简单,只有3个代码文件:

mainactivity.class 文件

package zhanghuan.cn.checkdesign;
 
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.widget.gridview;
import android.widget.textview;
 
import java.util.arraylist;
import java.util.calendar;
import java.util.list;
import java.util.locale;
 
public class mainactivity extends appcompatactivity {
 private list<checkbean> checkbeanlist;
 private myadapter madapter;
 private gridview mgridview;
 private textview monthtxt;
 
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  initdate();
 }
 
 private void initdate() {
 
  calendar calendar = calendar.getinstance(locale.china);
  int month = calendar.get(calendar.month) + 1;
  int day = calendar.getactualmaximum(calendar.date); // 获取当前月的天数
 
  checkbeanlist = new arraylist<checkbean>();
  for (int i = 0; i < day + 1; i++) {
   checkbean checkbean = new checkbean();
   if ((int) (math.random() * 20 % 4) == 3) {
    checkbean.day = i;
    checkbean.check_status = checkbean.checked;
   } else if ((int) (math.random() * 20 % 4) == 2) {
    checkbean.day = i;
    checkbean.check_status = checkbean.check_no;
   } else {
    checkbean.day = i;
    checkbean.check_status = checkbean.check_wait;
   }
 
   checkbeanlist.add(checkbean);
  }
 
  monthtxt = (textview)findviewbyid(r.id.item_month);
  if (month < 1 || month > 12) {
   month = 1;
  }
  monthtxt.settext("当前签到月份是:" + month);
 
  madapter = new myadapter(mainactivity.this);
  madapter.setlistdate(checkbeanlist);
 
  mgridview = (gridview) findviewbyid(r.id.main_gridview);
  mgridview.setadapter(madapter);
 }
}

适配器文件:myadapter.class 

package zhanghuan.cn.checkdesign;
 
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.imageview;
import android.widget.textview;
import android.widget.toast;
 
import java.util.list;
 
/**
 * created by zhanghuan on 2016/3/8.
 */
public class myadapter extends baseadapter {
 private context mcontext;
 private list<checkbean> checkbeanlist;
 
 public myadapter(context context) {
  mcontext = context;
 }
 
 public void setlistdate(list<checkbean> checklist) {
  checkbeanlist = checklist;
 }
 
 @override
 public int getcount() {
  return checkbeanlist.size();
 }
 
 @override
 public object getitem(int position) {
  return checkbeanlist.get(position);
 }
 
 @override
 public long getitemid(int position) {
  return position;
 }
 
 @override
 public view getview(final int position, view convertview, viewgroup parent) {
  final myhoder hoder;
 
  if (convertview == null) {
   convertview = layoutinflater.from(mcontext).inflate(r.layout.check_item, null);
   hoder = new myhoder();
   hoder.day = (textview) convertview.findviewbyid(r.id.item_day);
   hoder.status = (imageview) convertview.findviewbyid(r.id.item_image);
   convertview.settag(hoder);
  } else {
   hoder = (myhoder) convertview.gettag();
  }
 
  hoder.day.settext("day" + checkbeanlist.get(position).day);
  if (checkbeanlist.get(position).check_status == checkbean.check_no) {
   hoder.status.setimageresource(r.mipmap.check_no);
   hoder.status.setonclicklistener(new view.onclicklistener() {
    @override
    public void onclick(view v) {
     toast.maketext(mcontext, "恭喜你,签到成功!", toast.length_short).show();
     hoder.status.setimageresource(r.mipmap.checked);
     hoder.status.setclickable(false);
     checkbeanlist.get(position).check_status = checkbean.checked;
     /* 在此做一些签到请求的处理 */
    }
   });
  } else if (checkbeanlist.get(position).check_status == checkbean.check_wait) {
   hoder.status.setimageresource(r.mipmap.check_wait);
  } else if (checkbeanlist.get(position).check_status == checkbean.checked) {
   hoder.status.setimageresource(r.mipmap.checked);
  }
  return convertview;
 }
 
 private static class myhoder {
  textview day;
  imageview status;
 }
}

checkbean.class 文件:

package zhanghuan.cn.checkdesign;
 
import java.io.serializable;
 
/**
 * created by zhanghuan on 2016/3/8.
 */
public class checkbean implements serializable {
 // 根据自己的需求可以做补签的字段设置
 
 public static final int checked = 3; //已签到
 public static final int check_no = 2; //没有签到
 public static final int check_wait = 1; //等待签到 (时间没到无法签到)
 
 public int day;
 public int check_status;
}

源代码下载:点击打开链接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。