用gridview模仿日历并实现签到功能

最近 公司没事,闲得慌……(项目开发完了,是不是 都这样……^_^ 23333)

第二次写博客  大神勿喷 只针对 刚出生的猿猴们  谢谢。。。

前段时间公司要求在app上添加一个签到功能,也就是 什么签到领奖什么的,其实 那来什么奖啊 就是 送一些代金券 想促进消费……


首先,提到签到 估计 大部分人想到的都是日历, 我开始也在想 调用系统的日历做签到,然而 好像在系统日历上做更改有点复杂,反正我是不会的。所以 马上就想到了gridview 这个控件,只需要每排固定7个 好像就和日历一样了,而且可以随意定制。  好,就这样愉快的决定了 。action……


1、xml布局、 我把 星期几  单独列出来 排在顶部 是为了 让gridview 适配器 显得 简单一些。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"   
  6.     android:background="@color/white"  
  7.     >  
  8.       
  9.     <Button  
  10.         android:id="@+id/btn_sign_in"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:background="@drawable/btn_sign_in_pressd"  
  14.         android:layout_gravity="center_horizontal"  
  15.          android:layout_marginTop="30dp"  
  16.          android:text="签到"  
  17.          android:textColor="@color/white"  
  18.          android:textSize="18sp"  
  19.         />  
  20.       
  21.     <LinearLayout   
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:background="@color/white"  
  25.         android:gravity="center"  
  26.         android:orientation="vertical"  
  27.         android:layout_marginLeft="15dp"  
  28.         android:layout_marginRight="15dp"  
  29.          android:layout_marginTop="20dp"  
  30.          android:padding="10dp"  
  31.         >  
  32.       
  33.     <TextView  
  34.         android:id="@+id/text_date"  
  35.         android:layout_width="wrap_content"  
  36.         android:layout_height="wrap_content"  
  37.         android:textColor="@color/title_color"  
  38.         android:textSize="12sp"  
  39.         android:text="2015年11月30日"  
  40.         />  
  41.     <LinearLayout   
  42.         android:layout_marginTop="5dp"  
  43.         android:layout_width="match_parent"  
  44.         android:layout_height="wrap_content"  
  45.         android:orientation="horizontal"  
  46.         android:weightSum="7"  
  47.         >  
  48.         <TextView   
  49.             android:layout_width="match_parent"  
  50.             android:layout_height="wrap_content"  
  51.             android:layout_weight="1"  
  52.             android:text="周日"  
  53.             android:textColor="@color/title_color"  
  54.             android:textSize="10sp"  
  55.             android:gravity="center"  
  56.             />  
  57.         <TextView   
  58.             android:layout_width="match_parent"  
  59.             android:layout_height="wrap_content"  
  60.             android:layout_weight="1"  
  61.             android:text="周一"  
  62.             android:textColor="@color/title_color"  
  63.             android:textSize="10sp"  
  64.             android:gravity="center"  
  65.             />  
  66.         <TextView   
  67.             android:layout_width="match_parent"  
  68.             android:layout_height="wrap_content"  
  69.             android:layout_weight="1"  
  70.             android:text="周二"  
  71.             android:textColor="@color/title_color"  
  72.             android:textSize="10sp"  
  73.             android:gravity="center"  
  74.             />  
  75.         <TextView   
  76.             android:layout_width="match_parent"  
  77.             android:layout_height="wrap_content"  
  78.             android:layout_weight="1"  
  79.             android:text="周三"  
  80.             android:textColor="@color/title_color"  
  81.             android:textSize="10sp"  
  82.             android:gravity="center"  
  83.             />  
  84.         <TextView   
  85.             android:layout_width="match_parent"  
  86.             android:layout_height="wrap_content"  
  87.             android:layout_weight="1"  
  88.             android:text="周四"  
  89.             android:textColor="@color/title_color"  
  90.             android:textSize="10sp"  
  91.             android:gravity="center"  
  92.             />  
  93.         <TextView   
  94.             android:layout_width="match_parent"  
  95.             android:layout_height="wrap_content"  
  96.             android:layout_weight="1"  
  97.             android:text="周五"  
  98.             android:textColor="@color/title_color"  
  99.             android:textSize="10sp"  
  100.             android:gravity="center"  
  101.             />  
  102.         <TextView   
  103.             android:layout_width="match_parent"  
  104.             android:layout_height="wrap_content"  
  105.             android:layout_weight="1"  
  106.             android:text="周六"  
  107.             android:textColor="@color/title_color"  
  108.             android:textSize="10sp"  
  109.             android:gravity="center"  
  110.             />  
  111.           
  112.     </LinearLayout>  
  113.       
  114.      <com.example.signDate.MyGridViewR  
  115.                     android:id="@+id/grid_date"  
  116.                     android:layout_width="match_parent"  
  117.                     android:layout_height="wrap_content"  
  118.                     android:background="@color/white"  
  119.                     android:horizontalSpacing="1dp"  
  120.                     android:numColumns="7"  
  121.                     android:scrollbars="none"  
  122.                     android:verticalSpacing="5dp"   
  123.                     android:layout_marginTop="10dp"  
  124.                     >  
  125.                 </com.example.signDate.MyGridViewR>  
  126.       
  127.      <Button   
  128.          android:id="@+id/next_day"  
  129.          android:layout_width="wrap_content"  
  130.          android:layout_height="wrap_content"  
  131.          android:text="穿越到未来(+1天)"  
  132.          android:layout_marginTop="10dp"  
  133.          />  
  134.        
  135.     </LinearLayout>  
  136.       
  137.       
  138. </LinearLayout>  

2、然后 activity 代码  主要是 生成一些模拟数据 和 对签到过后 对页面的 一些操作 ,其中 对点击签到 和获得数据的方法中,可以加入自己的 逻辑代码和http请求。

  1. package com.example.signDate;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.ArrayList;  
  5. import java.util.Calendar;  
  6.   
  7. import android.os.Bundle;  
  8. import android.util.TypedValue;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. import com.example.gridviewsigndate.R;  
  16. /**  
  17.  *   
  18.  * @author zhangyao  
  19.  *  
  20.  */  
  21. public class SignInActivity extends BaseActivity implements OnClickListener{  
  22.       
  23.     private TextView textDate;  
  24.       
  25.     private MyGridViewR gridDate;    
  26.       
  27.     private DateSignInAdapter dateAdapter;  
  28.       
  29.     private Button btnSignin;    
  30.       
  31.     private String dateStr ; // 用于显示几年几月 判断  当月1号 为星期几  
  32.       
  33.     private ArrayList<String> listDate = new ArrayList<String>();  
  34.   
  35.     private Calendar now = Calendar.getInstance(); //当前时间  
  36.       
  37.     private int count = 0;  
  38.       
  39.     private Button nextDay;// 穿越。。。。。:)  
  40.       
  41.       
  42.     private int mMonth = 0;  
  43.       
  44.     @Override  
  45.     protected void onCreate(Bundle arg0) {  
  46.         // TODO Auto-generated method stub  
  47.         super.onCreate(arg0);  
  48.         setContentView(R.layout.activity_sign_in);  
  49.           
  50.         initView();  
  51.           
  52.         for (int i = 0; i < 31; i++) { //暂时默认一个月31天  0代表未签到 1 代表签到  应该从服务器获取 实际的信息,这里模拟   
  53.               
  54.             listDate.add("0");  
  55.               
  56.         }  
  57.           
  58.         getSignInfo();  
  59.     }  
  60.   
  61.     private void initView() {  
  62.         // TODO Auto-generated method stub  
  63.         textDate = (TextView) findViewById(R.id.text_date);  
  64.         gridDate = (MyGridViewR) findViewById(R.id.grid_date);  
  65.         btnSignin =  (Button) findViewById(R.id.btn_sign_in);  
  66.           
  67.         nextDay  =  (Button) findViewById(R.id.next_day);  
  68.           
  69.         btnSignin.setOnClickListener(this);  
  70.           
  71.         nextDay.setOnClickListener(this);  
  72.     }  
  73.       
  74.     /**  
  75.      * 获取签到信息  
  76.      */  
  77.     private void getSignInfo() {  
  78.         //根据实际情况可以从后台 获取 当月的天数 星期几 和 签到的连续天数等信息   
  79.           
  80.         dateStr = new String();  
  81.           
  82.         dateStr = now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH)+1)+"-01";   
  83.           
  84.         textDate.setText(now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH)+1)+"-"+now.get(Calendar.DAY_OF_MONTH));  
  85.           
  86.         setDate(dateStr);  
  87.     }  
  88.   
  89.     protected void setDate(String dateStr) {  
  90.         // TODO Auto-generated method stub  
  91.           
  92.         int start = 0; //用 每月的一号是周几  来判断 从哪个 位移量 开始计算  
  93.           
  94.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  95.           
  96.         Calendar date = Calendar.getInstance();  
  97.         try {  
  98.             date.setTime(format.parse(dateStr));  
  99.               
  100.             switch (date.get(Calendar.DAY_OF_WEEK)) {  
  101.             case 1: //周日  
  102.                   
  103.                 start = 0;  
  104.                   
  105.                 break;  
  106.             case 2: //周一  
  107.                   
  108.                 start = 1;  
  109.                   
  110.                 break;  
  111.             case 3:  
  112.                   
  113.                 start = 2;  
  114.                   
  115.                 break;  
  116.             case 4:  
  117.                   
  118.                 start = 3;  
  119.                   
  120.                 break;  
  121.             case 5:  
  122.                   
  123.                 start = 4;  
  124.                   
  125.                 break;  
  126.             case 6:  
  127.                   
  128.                 start = 5;  
  129.                   
  130.                 break;  
  131.             case 7:  
  132.                   
  133.                 start = 6;  
  134.                   
  135.                 break;  
  136.             default:  
  137.                 break;  
  138.             }  
  139.               
  140.               
  141.         } catch (Exception e) {  
  142.             // TODO: handle exception  
  143.         }  
  144.           
  145.         ArrayList<String> lists = new ArrayList<String>();  
  146.         lists.clear();  
  147.         count = 0;  
  148.         for (int i = 0; i < listDate.size(); i++) {  
  149.               
  150.             lists.add(listDate.get(i));  
  151.             if ("1".equals(listDate.get(i))) { //计算 已经签到的天数  
  152.                   
  153.                 count++;  
  154.                   
  155.             }  
  156.         }  
  157.           
  158.         dateAdapter = new DateSignInAdapter(SignInActivity.this, lists,start);  
  159.         gridDate.setAdapter(dateAdapter);  
  160.         dateAdapter.notifyDataSetChanged();  
  161.           
  162.         //当天已签到  
  163.         if ("1".equals(lists.get(Integer.valueOf(now.get(Calendar.DAY_OF_MONTH)) - 1))) {  
  164.               
  165.             btnSignin.setTextSize(TypedValue.COMPLEX_UNIT_SP , 12);  
  166.             btnSignin.setText("已签到"+count+"次");  
  167.               
  168.         }  
  169.         else {  
  170.             btnSignin.setTextSize(TypedValue.COMPLEX_UNIT_SP , 18);  
  171.             btnSignin.setText("签到");  
  172.         }  
  173.           
  174.     }  
  175.   
  176.       
  177.   
  178.     @Override  
  179.     public void onClick(View v) {  
  180.           
  181.         if (v == btnSignin) {  
  182.               
  183.             if ("0".equals(listDate.get(Integer.valueOf(now.get(Calendar.DAY_OF_MONTH)) - 1))) {  
  184.                   
  185.                 Signin(now.get(Calendar.DAY_OF_MONTH));  
  186.                   
  187.             }  
  188.             else {  
  189.                 Toast.makeText(getApplicationContext(), "今天已经签到过了", Toast.LENGTH_SHORT).show();;  
  190.             }  
  191.               
  192.         }  
  193.         if (v == nextDay) {  
  194.               
  195.             now.add(Calendar.DATE, 1); // +1天  
  196.               
  197.             //处理穿越到 下一个月  把签到数据清零。。。  
  198.             int month = now.get(Calendar.MONTH)+1;  
  199.               
  200.             if (mMonth == 0) {  
  201.                   
  202.                 mMonth = month;  
  203.             }  
  204.             else {  
  205.                 if (mMonth != month) {  
  206.                       
  207.                     listDate.clear();  
  208.                     for (int i = 0; i < 31; i++) { //暂时默认一个月31天  0代表未签到 1 代表签到  应该从服务器获取 实际的信息,这里模拟   
  209.                           
  210.                         listDate.add("0");  
  211.                           
  212.                     }  
  213.                     mMonth = month;  
  214.                 }  
  215.                   
  216.             }  
  217.               
  218.             getSignInfo();  
  219.               
  220.         }  
  221.           
  222.     }  
  223.   
  224.     /**  
  225.      * 签到  
  226.      * @param day   
  227.      */  
  228.     private void Signin(int day) {  
  229.         //发送http 请求 到服务器 根据返回值 判断是否签到 成功, 这里本地模拟。。  
  230.           
  231.         listDate.set(day-1, "1");  
  232.         getSignInfo();  
  233.           
  234.     }  
  235. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值