亲宝软件园·资讯

展开

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

langtuteng136 人气:0

先上一张效果图:

其中这些签到的效果图是在网上找的,然后重要用到的控件就是 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;
}

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

加载全部内容

相关教程
猜你喜欢
用户评论