这篇文章主要为大家详细介绍了Android可签到日历控件的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
正文
Android可签到日历控件的实现方法
最近在公司的功能需求中,需要实现可以签到的日历,签到后在签到过的日期做标志。本功能参考了网上一些大神的日历控件,在此基础上进行修改,已满足本公司的需求,现已完成,记录一下。
布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "@color/white" android:orientation = "vertical" > < LinearLayout android:id = "@+id/i8show_attention_rl" android:layout_width = "match_parent" android:layout_height = "60dp" android:orientation = "horizontal" > < RelativeLayout android:layout_width = "match_parent" android:layout_height = "60dp" android:background = "@color/white" android:gravity = "center_vertical" android:padding = "@dimen/I8d6" > < ImageView android:id = "@+id/i8show_attention_back" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerVertical = "true" android:src = "@drawable/i8live_nav_bcak" /> < TextView android:id = "@+id/i8show_attention_tittle" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:ellipsize = "end" android:gravity = "center" android:singleLine = "true" android:text = "我的签到" android:textColor = "@android:color/black" android:textSize = "@dimen/i8_text_size_18" /> </ RelativeLayout > </ LinearLayout > < RelativeLayout android:id = "@+id/rl_current_date" android:layout_width = "match_parent" android:layout_height = "49dp" android:layout_below = "@id/i8show_attention_rl" > < TextView android:id = "@+id/tv_sign_year_month" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerVertical = "true" android:layout_marginLeft = "60dp" android:text = "2017年11月" android:textColor = "#000000" android:textSize = "17sp" /> </ RelativeLayout > < com.i8dayou.an.zb.userinfo.signcalendar.SignCalendar android:id = "@+id/sc_main" android:layout_width = "match_parent" android:layout_height = "320dp" android:layout_below = "@id/rl_current_date" android:layout_marginLeft = "15dp" android:layout_marginRight = "15dp" android:layout_marginTop = "15dp" android:clickable = "true" ></ com.i8dayou.an.zb.userinfo.signcalendar.SignCalendar > < RelativeLayout android:id = "@+id/rl_btn_sign" android:layout_width = "135dp" android:layout_height = "49dp" android:layout_centerInParent = "true" android:layout_marginBottom = "50dp" android:layout_alignParentBottom = "true" android:background = "@drawable/btn_sign_calendar" android:orientation = "horizontal" > < TextView android:id = "@+id/btn_sign" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:layout_marginLeft = "15dp" android:layout_marginRight = "15dp" android:text = "签 到" android:textColor = "#fff" android:textSize = "17sp" /> </ RelativeLayout > < RelativeLayout android:id = "@+id/rl_get_gift_view" android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "#dd000000" android:visibility = "gone" android:clickable = "false" > < ImageView android:id = "@+id/iv_huode" android:layout_width = "229dp" android:layout_height = "59dp" android:layout_centerHorizontal = "true" android:layout_marginTop = "120dp" android:src = "@drawable/i8live_huoede" /> < RelativeLayout android:id = "@+id/rl_sun" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@id/iv_huode" android:layout_centerHorizontal = "true" > < ImageView android:id = "@+id/iv_sun_bg" android:layout_width = "200dp" android:layout_height = "200dp" android:layout_centerInParent = "true" android:src = "@drawable/signed_on_rotate_bg" /> < ImageView android:id = "@+id/iv_sun" android:layout_width = "98dp" android:layout_height = "95dp" android:layout_centerInParent = "true" android:src = "@drawable/i8live_sun" /> </ RelativeLayout > < RelativeLayout android:id = "@+id/rl_text_info" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@id/rl_sun" android:layout_marginTop = "10dp" > < TextView android:id = "@+id/tv_text_one" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerHorizontal = "true" android:layout_marginLeft = "20dp" android:layout_marginRight = "20dp" android:text = "第一天签到可获得20个人气币,经验200,免费弹幕1次" android:textColor = "#ffffff" android:textSize = "13sp" /> </ RelativeLayout > < RelativeLayout android:id = "@+id/rl_queding_btn" android:layout_width = "218dp" android:layout_height = "38dp" android:layout_below = "@id/rl_text_info" android:layout_centerHorizontal = "true" android:layout_marginTop = "19dp" android:background = "@drawable/online_get_gift_btn" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:text = "确定" android:textColor = "#ffffff" android:textSize = "16sp" /> </ RelativeLayout > </ RelativeLayout > </ RelativeLayout > |
主界面Activity代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
public class SignCalendarActivity extends Activity { private SignCalendar calendar; private String date; private TextView btn_sign; private TextView tv_sign_year_month; private SignCalendarReq signCalendarReq; private SignCalendarReq.DataBean dataBean; List<String> list = new ArrayList<String>(); private int month; private int year; private RelativeLayout rlGetGiftData; private TextView tvGetSunValue; private ImageView ivSun; private ImageView ivSunBg; private RelativeLayout rlQuedingBtn; private RelativeLayout rlBtnSign; private ImageView signBack; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_sign_calendar); //接收传递过来的初始化数据 SignCalendarReq signCalendarReq = (SignCalendarReq) getIntent().getSerializableExtra( "userInfos" ); month = Calendar.getInstance().get(Calendar.MONTH); year = Calendar.getInstance().get(Calendar.YEAR); SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd" ); Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间 date = formatter.format(curDate); calendar = (SignCalendar) findViewById(R.id.sc_main); btn_sign = (TextView) findViewById(R.id.btn_sign); tv_sign_year_month = (TextView) findViewById(R.id.tv_sign_year_month); rlGetGiftData = (RelativeLayout) findViewById(R.id.rl_get_gift_view); tvGetSunValue = (TextView) findViewById(R.id.tv_text_one); ivSun = (ImageView) findViewById(R.id.iv_sun); ivSunBg = (ImageView) findViewById(R.id.iv_sun_bg); signBack = (ImageView) findViewById(R.id.i8show_attention_back); rlQuedingBtn = (RelativeLayout) findViewById(R.id.rl_queding_btn); rlBtnSign = (RelativeLayout) findViewById(R.id.rl_btn_sign); tv_sign_year_month.setText(year + "年" + (month + 1 ) + "月" ); //设置日期 if (signCalendarReq != null ) { if (signCalendarReq.getState().getCode() == 1 ) { dataBean = signCalendarReq.getData(); String signDay = dataBean.getSignDay(); String[] splitDay = signDay.split( "," ); for ( int i = 0 ; i < splitDay.length; i++) { if (Integer.parseInt(splitDay[i]) < 10 ) { list.add(year + "-" + (month + 1 ) + "-0" + splitDay[i]); } else { list.add(year + "-" + (month + 1 ) + "-" + splitDay[i]); } } calendar.addMarks(list, 0 ); if (dataBean.getIsSign() == 1 ) { //1是已签到,0是未签到 rlBtnSign.setBackgroundResource(R.drawable.btn_sign_calendar_no); btn_sign.setText( "已签到" ); rlBtnSign.setClickable( false ); } else { rlBtnSign.setBackgroundResource(R.drawable.btn_sign_calendar); btn_sign.setText( "签 到" ); } } } btn_sign.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { initSignCalendarData( 2 ); //判断是否签到成功接口 } }); rlQuedingBtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { rlGetGiftData.setVisibility(View.GONE); } }); signBack.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } //点击签到按钮网络请求数据 private void initSignCalendarData( int requestType) { Map<String, String> map = new HashMap<String, String>(); map.put( "uid" , I8ShowSharePre.getHomeId(getApplicationContext())); map.put( "token" , I8ShowSharePre.getToken(getApplicationContext())); map.put( "type" , requestType + "" ); I8ShowHttpEngine.getInstance().postNotJson(I8ShowAppClient.QUERY_GET_SIGN_CALENDAR_RESULT, map, new I8ShowHttpEngine.Listener() { @Override public void onResponseSuccess(Object data) { Log.i( "aaa" , "签到数据:" + data.toString()); IsSignSuccessReq isSignSuccessReq = new Gson().fromJson(data.toString(), IsSignSuccessReq. class ); if (isSignSuccessReq.getState().getCode() == 1 ) { rlGetGiftData.setVisibility(View.VISIBLE); rlBtnSign.setBackgroundResource(R.drawable.btn_sign_calendar_no); btn_sign.setText( "已签到" ); if (isSignSuccessReq.getData().getValueType() == 0 ) { //0是sun ivSun.setImageResource(R.drawable.i8live_sun); tvGetSunValue.setText( "恭喜获得" + isSignSuccessReq.getData().getAddValue() + "个" ); I8ShowUserInfoSP.setSunValue(getApplicationContext(), isSignSuccessReq.getData().getAllValue()); //保存领取礼物后的数据 } else if ((isSignSuccessReq.getData().getValueType() == 1 )) { //1是zuan ivSun.setImageResource(R.drawable.i8live_zuan_sign); tvGetSunValue.setText( "恭喜获得" + isSignSuccessReq.getData().getAddValue() + "个" ); I8SP.setDiamonds(getApplicationContext(), isSignSuccessReq.getData().getAllValue()); } Animation operatingAnim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_anim_online_gift); LinearInterpolator lin = new LinearInterpolator(); operatingAnim.setInterpolator(lin); ivSunBg.startAnimation(operatingAnim); //list.add("2017-11-18"); list.add(date); // calendar.setCalendarDaysBgColor(list,R.drawable.bg_sign_today); calendar.addMarks(list, 0 ); } } @Override public void onResponseFailed(String failedMsg) { Log.i( "aaa" , "签到数据:" + failedMsg); } }); } } |
日历自定义控件代码:
发表评论