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

来源:互联网 发布:mac充电器怎么拆开 编辑:程序博客网 时间:2024/06/05 17:09

最近 公司没事,闲得慌……(项目开发完了,是不是 都这样……^_^ 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. }  

原创粉丝点击