Android簡(jiǎn)單實(shí)現(xiàn)app每月簽到功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)app每月簽到功能的具體代碼,供大家參考,具體內(nèi)容如下
先上一張效果圖:
其中這些簽到的效果圖是在網(wǎng)上找的,然后重要用到的控件就是 GridvVew 了, 代碼很簡(jiǎn)單,只有3個(gè)代碼文件:
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); // 獲取當(dāng)前月的天數(shù) 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("當(dāng)前簽到月份是:" + 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; /* 在此做一些簽到請(qǐng)求的處理 */ } }); } 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 { // 根據(jù)自己的需求可以做補(bǔ)簽的字段設(shè)置 public static final int CHECKED = 3; //已簽到 public static final int CHECK_NO = 2; //沒(méi)有簽到 public static final int CHECK_WAIT = 1; //等待簽到 (時(shí)間沒(méi)到無(wú)法簽到) public int day; public int check_status; }
源代碼下載:點(diǎn)擊打開(kāi)鏈接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼(附源碼)
這篇文章主要介紹了Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Android編程實(shí)現(xiàn)屏幕自適應(yīng)方向尺寸與分辨率的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)屏幕自適應(yīng)方向尺寸與分辨率的方法,涉及Android屏幕分辨率、布局、橫豎屏切換等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12Android對(duì)EditTex的圖片實(shí)現(xiàn)監(jiān)聽(tīng)
這篇文章主要為大家詳細(xì)介紹了Android如何對(duì)EditTex的圖片實(shí)現(xiàn)監(jiān)聽(tīng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android開(kāi)發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問(wèn)題分析
這篇文章主要介紹了Android開(kāi)發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問(wèn)題,結(jié)合實(shí)例形式分析了Android三角函數(shù)運(yùn)算中的弧度與角度計(jì)算問(wèn)題與相關(guān)解決方法,需要的朋友可以參考下2017-11-11Android中利用viewflipper動(dòng)畫切換屏幕效果
這篇文章主要介紹了Android中利用viewflipper動(dòng)畫切換屏幕效果的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android實(shí)現(xiàn) EditText輸入手機(jī)號(hào)空格功能
這篇文章主要介紹了Android實(shí)現(xiàn) EditText輸入手機(jī)號(hào)空格功能,實(shí)現(xiàn)思路是要重寫TextWatcher,每次EditText內(nèi)容變化,都判斷內(nèi)容是否符合要求,具體實(shí)例代碼大家參考下本文2018-02-02Rxjava2_Flowable_Sqlite_Android數(shù)據(jù)庫(kù)訪問(wèn)實(shí)例
下面小編就為大家分享一篇Rxjava2_Flowable_Sqlite_Android數(shù)據(jù)庫(kù)訪問(wèn)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02