最近 公司没事,闲得慌……(项目开发完了,是不是 都这样……^_^ 23333)
第二次写博客 大神勿喷 只针对 刚出生的猿猴们 谢谢。。。
前段时间公司要求在app上添加一个签到功能,也就是 什么签到领奖什么的,其实 那来什么奖啊 就是 送一些代金券 想促进消费……
首先,提到签到 估计 大部分人想到的都是日历, 我开始也在想 调用系统的日历做签到,然而 好像在系统日历上做更改有点复杂,反正我是不会的。所以 马上就想到了gridview 这个控件,只需要每排固定7个 好像就和日历一样了,而且可以随意定制。 好,就这样愉快的决定了 。action……
1、xml布局、 我把 星期几 单独列出来 排在顶部 是为了 让gridview 适配器 显得 简单一些。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="@color/white"
- >
- <Button
- android:id="@+id/btn_sign_in"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/btn_sign_in_pressd"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="30dp"
- android:text="签到"
- android:textColor="@color/white"
- android:textSize="18sp"
- />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/white"
- android:gravity="center"
- android:orientation="vertical"
- android:layout_marginLeft="15dp"
- android:layout_marginRight="15dp"
- android:layout_marginTop="20dp"
- android:padding="10dp"
- >
- <TextView
- android:id="@+id/text_date"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="@color/title_color"
- android:textSize="12sp"
- android:text="2015年11月30日"
- />
- <LinearLayout
- android:layout_marginTop="5dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:weightSum="7"
- >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="周日"
- android:textColor="@color/title_color"
- android:textSize="10sp"
- android:gravity="center"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="周一"
- android:textColor="@color/title_color"
- android:textSize="10sp"
- android:gravity="center"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="周二"
- android:textColor="@color/title_color"
- android:textSize="10sp"
- android:gravity="center"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="周三"
- android:textColor="@color/title_color"
- android:textSize="10sp"
- android:gravity="center"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="周四"
- android:textColor="@color/title_color"
- android:textSize="10sp"
- android:gravity="center"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="周五"
- android:textColor="@color/title_color"
- android:textSize="10sp"
- android:gravity="center"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="周六"
- android:textColor="@color/title_color"
- android:textSize="10sp"
- android:gravity="center"
- />
- </LinearLayout>
- <com.example.signDate.MyGridViewR
- android:id="@+id/grid_date"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/white"
- android:horizontalSpacing="1dp"
- android:numColumns="7"
- android:scrollbars="none"
- android:verticalSpacing="5dp"
- android:layout_marginTop="10dp"
- >
- </com.example.signDate.MyGridViewR>
- <Button
- android:id="@+id/next_day"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="穿越到未来(+1天)"
- android:layout_marginTop="10dp"
- />
- </LinearLayout>
- </LinearLayout>
-
- package com.example.signDate;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import android.os.Bundle;
- import android.util.TypedValue;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.example.gridviewsigndate.R;
- /**
- *
- * @author zhangyao
- *
- */
- public class SignInActivity extends BaseActivity implements OnClickListener{
- private TextView textDate;
- private MyGridViewR gridDate;
- private DateSignInAdapter dateAdapter;
- private Button btnSignin;
- private String dateStr ; // 用于显示几年几月 判断 当月1号 为星期几
- private ArrayList<String> listDate = new ArrayList<String>();
- private Calendar now = Calendar.getInstance(); //当前时间
- private int count = 0;
- private Button nextDay;// 穿越。。。。。:)
- private int mMonth = 0;
- @Override
- protected void onCreate(Bundle arg0) {
- // TODO Auto-generated method stub
- super.onCreate(arg0);
- setContentView(R.layout.activity_sign_in);
- initView();
- for (int i = 0; i < 31; i++) { //暂时默认一个月31天 0代表未签到 1 代表签到 应该从服务器获取 实际的信息,这里模拟
- listDate.add("0");
- }
- getSignInfo();
- }
- private void initView() {
- // TODO Auto-generated method stub
- textDate = (TextView) findViewById(R.id.text_date);
- gridDate = (MyGridViewR) findViewById(R.id.grid_date);
- btnSignin = (Button) findViewById(R.id.btn_sign_in);
- nextDay = (Button) findViewById(R.id.next_day);
- btnSignin.setOnClickListener(this);
- nextDay.setOnClickListener(this);
- }
- /**
- * 获取签到信息
- */
- private void getSignInfo() {
- //根据实际情况可以从后台 获取 当月的天数 星期几 和 签到的连续天数等信息
- dateStr = new String();
- dateStr = now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH)+1)+"-01";
- textDate.setText(now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH)+1)+"-"+now.get(Calendar.DAY_OF_MONTH));
- setDate(dateStr);
- }
- protected void setDate(String dateStr) {
- // TODO Auto-generated method stub
- int start = 0; //用 每月的一号是周几 来判断 从哪个 位移量 开始计算
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Calendar date = Calendar.getInstance();
- try {
- date.setTime(format.parse(dateStr));
- switch (date.get(Calendar.DAY_OF_WEEK)) {
- case 1: //周日
- start = 0;
- break;
- case 2: //周一
- start = 1;
- break;
- case 3:
- start = 2;
- break;
- case 4:
- start = 3;
- break;
- case 5:
- start = 4;
- break;
- case 6:
- start = 5;
- break;
- case 7:
- start = 6;
- break;
- default:
- break;
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- ArrayList<String> lists = new ArrayList<String>();
- lists.clear();
- count = 0;
- for (int i = 0; i < listDate.size(); i++) {
- lists.add(listDate.get(i));
- if ("1".equals(listDate.get(i))) { //计算 已经签到的天数
- count++;
- }
- }
- dateAdapter = new DateSignInAdapter(SignInActivity.this, lists,start);
- gridDate.setAdapter(dateAdapter);
- dateAdapter.notifyDataSetChanged();
- //当天已签到
- if ("1".equals(lists.get(Integer.valueOf(now.get(Calendar.DAY_OF_MONTH)) - 1))) {
- btnSignin.setTextSize(TypedValue.COMPLEX_UNIT_SP , 12);
- btnSignin.setText("已签到"+count+"次");
- }
- else {
- btnSignin.setTextSize(TypedValue.COMPLEX_UNIT_SP , 18);
- btnSignin.setText("签到");
- }
- }
- @Override
- public void onClick(View v) {
- if (v == btnSignin) {
- if ("0".equals(listDate.get(Integer.valueOf(now.get(Calendar.DAY_OF_MONTH)) - 1))) {
- Signin(now.get(Calendar.DAY_OF_MONTH));
- }
- else {
- Toast.makeText(getApplicationContext(), "今天已经签到过了", Toast.LENGTH_SHORT).show();;
- }
- }
- if (v == nextDay) {
- now.add(Calendar.DATE, 1); // +1天
- //处理穿越到 下一个月 把签到数据清零。。。
- int month = now.get(Calendar.MONTH)+1;
- if (mMonth == 0) {
- mMonth = month;
- }
- else {
- if (mMonth != month) {
- listDate.clear();
- for (int i = 0; i < 31; i++) { //暂时默认一个月31天 0代表未签到 1 代表签到 应该从服务器获取 实际的信息,这里模拟
- listDate.add("0");
- }
- mMonth = month;
- }
- }
- getSignInfo();
- }
- }
- /**
- * 签到
- * @param day
- */
- private void Signin(int day) {
- //发送http 请求 到服务器 根据返回值 判断是否签到 成功, 这里本地模拟。。
- listDate.set(day-1, "1");
- getSignInfo();
- }
- }