您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。