这篇文章将为大家详细讲解有关使用Android怎么实现一个每月签到功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
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; 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; }
关于使用Android怎么实现一个每月签到功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。