Android實現點擊簽到按鈕直接簽到,彈出dialog,先上效果圖
demo是利用gridview實現的,現附上布局文件
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
171
172
173
174
175
176
177
|
<? 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" > < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:padding = "2dp" android:orientation = "vertical" android:background = "@drawable/shape_btn_white" > < RelativeLayout android:layout_width = "match_parent" android:layout_height = "40dp" > < ImageView android:id = "@+id/iv_front" android:layout_width = "30dp" android:layout_height = "40dp" android:padding = "10dp" android:layout_marginLeft = "8dp" android:scaleType = "centerCrop" android:layout_alignParentLeft = "true" android:layout_centerInParent = "true" android:src = "@mipmap/icon_sign_front" /> < TextView android:id = "@+id/tv_date" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:textColor = "#2fbbef" android:textSize = "15sp" android:text = "2016-7-16" /> < ImageView android:id = "@+id/iv_next" android:layout_width = "30dp" android:layout_height = "40dp" android:layout_marginRight = "8dp" android:padding = "10dp" android:scaleType = "centerCrop" android:layout_alignParentRight = "true" android:layout_centerInParent = "true" android:src = "@mipmap/icon_sign_next" /> </ RelativeLayout > < LinearLayout android:layout_width = "match_parent" android:layout_height = "40dp" > < TextView android:layout_width = "40dp" android:layout_height = "40dp" android:layout_weight = "1" android:gravity = "center" android:text = "日" android:textSize = "15sp" android:textColor = "#888" /> < TextView android:layout_width = "40dp" android:layout_height = "40dp" android:layout_weight = "1" android:gravity = "center" android:text = "一" android:textSize = "15sp" android:textColor = "#888" /> < TextView android:layout_width = "40dp" android:layout_height = "40dp" android:layout_weight = "1" android:gravity = "center" android:text = "二" android:textSize = "15sp" android:textColor = "#888" /> < TextView android:layout_width = "40dp" android:layout_height = "40dp" android:layout_weight = "1" android:gravity = "center" android:text = "三" android:textSize = "15sp" android:textColor = "#888" /> < TextView android:layout_width = "40dp" android:layout_height = "40dp" android:layout_weight = "1" android:gravity = "center" android:text = "四" android:textSize = "15sp" android:textColor = "#888" /> < TextView android:layout_width = "40dp" android:layout_height = "40dp" android:layout_weight = "1" android:gravity = "center" android:text = "五" android:textSize = "15sp" android:textColor = "#888" /> < TextView android:layout_width = "40dp" android:layout_height = "40dp" android:layout_weight = "1" android:gravity = "center" android:text = "六" android:textSize = "15sp" android:textColor = "#888" /> </ LinearLayout > < GridView android:id = "@+id/gv_sign_date" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:background = "#dbdbdb" android:clickable = "true" android:clipChildren = "true" android:columnWidth = "30dp" android:listSelector = "@null" android:numColumns = "7" android:paddingBottom = "1dp" android:stretchMode = "columnWidth" android:verticalSpacing = "1dp" ></ GridView > < TextView android:layout_width = "match_parent" android:layout_height = "1dp" android:background = "#dbdbdb" /> < LinearLayout android:layout_width = "match_parent" android:layout_height = "50dp" android:gravity = "center" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "連續簽到會有更多積分哦 !" android:textColor = "#2a2a2a" android:layout_marginRight = "5dp" android:textSize = "14sp" /> < TextView android:id = "@+id/tv_sign_num" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginRight = "20dp" android:visibility = "gone" android:text = "3天" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/jifen" android:textColor = "#2a2a2a" android:visibility = "gone" android:layout_marginRight = "5dp" android:textSize = "14sp" /> < TextView android:id = "@+id/tv_jifen_num" android:visibility = "gone" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "100" /> </ LinearLayout > </ LinearLayout > </ LinearLayout > |
效果圖:
dialog中的主要代碼:
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
|
public class SignDataDialog extends Dialog implements View.OnClickListener{ private GridView gridView; private ImageView iv_front,iv_next; private TextView tv_date,tv_sign_days,tv_jifen; private MyCalendarAdapter adapter; private SpecialCalendar sp; private Context context; private SignDateModle modle; private SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-M-d" ); private String systime; private int year,month; String[] b= new String[]{ "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" }; public SignDataDialog(Context context,SignDateModle modle) { super (context); this .context=context; this .modle=modle; setContentView(R.layout.dialog_sign_data); Window window = getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.gravity = Gravity.CENTER; window.setBackgroundDrawableResource(android.R.color.transparent); window.setAttributes(params); setCanceledOnTouchOutside( true ); } @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); sp= new SpecialCalendar(); Date date = new Date(); systime = sdf.format(date); year=Integer.valueOf(systime.split( "-" )[ 0 ]); month=Integer.valueOf(systime.split( "-" )[ 1 ]); iv_front= (ImageView) findViewById(R.id.iv_front); iv_next= (ImageView) findViewById(R.id.iv_next); gridView= (GridView) findViewById(R.id.gv_sign_date); tv_date= (TextView) findViewById(R.id.tv_date); tv_sign_days= (TextView) findViewById(R.id.tv_sign_num); tv_jifen= (TextView) findViewById(R.id.tv_jifen_num); tv_date.setText(systime); iv_front.setOnClickListener( this ); iv_next.setOnClickListener( this ); if (modle!= null ) { String dates=modle.getDateTime(); adapter = new MyCalendarAdapter(context, Integer.valueOf(dates.split( "-" )[ 0 ]), Integer.valueOf(dates.split( "-" )[ 1 ]), 17 , getStrings(modle)); gridView.setAdapter(adapter); } } public String[] getStrings(SignDateModle modle){ int year=Integer.valueOf(modle.getDateTime().split( "-" )[ 0 ]); int moth=Integer.valueOf(modle.getDateTime().split( "-" )[ 1 ]); int start=sp.getWeekdayOfMonth(year,moth); for ( int i= 0 ;i<modle.getDatas().size();i++){ if (modle.getDatas().get(i).getStatus()== 1 ){ b[i]=String.valueOf( 1 ); } } return b; } @Override public void onClick(View v) { switch (v.getId()){ case R.id.iv_front: redMonth(); tv_date.setText(year+ "-" +month+ "-" +systime.split( "-" )[ 2 ]); changeOtherMoth(year, month); break ; case R.id.iv_next: addMonth(); tv_date.setText(year+ "-" +month+ "-" +systime.split( "-" )[ 2 ]); changeOtherMoth(year,month); break ; } } public void addMonth(){ month++; if (month== 13 ){ month= 1 ; year++; } } public void redMonth(){ month--; if (month== 0 ){ month= 12 ; year--; } } public void changeOtherMoth( int year, int moth){ if (CMethod.isNet(context)){ JSONObject jsonObject= NetJsonModle.getJsonObject(context, "528" ); try { jsonObject.put( "reporterId" , new LastLoginUtils(context).getReporterId()); jsonObject.put( "time" , year + "-" + moth); HttpUtils.PostDataToWeb(UrlAddressUrils.CODE_OTHER, AppConstants.SIGN_DATA_INFO, jsonObject, new HttpClientListener() { @Override public void onSuccess(String result) { Gson gson = new Gson(); SignDateModle m = gson.fromJson(result, SignDateModle. class ); for ( int i = 0 ; i < b.length; i++) { b[i] = "0" ; } if (m != null ) { String dates = m.getDateTime(); adapter = new MyCalendarAdapter(context, Integer.valueOf(dates.split( "-" )[ 0 ]), Integer.valueOf(dates.split( "-" )[ 1 ]), 17 , getStrings(m)); gridView.setAdapter(adapter); } } @Override public void onFailure(String result) { } @Override public void onError() { } }); } catch (JSONException e) { e.printStackTrace(); } } else { T.s( "請檢查網絡是否連接" ); } } |
其計算日期的主要代碼在適配器中:
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
class MyCalendarAdapter extends BaseAdapter{ private boolean isLeapyear = false ; // 是否為閏年 private int daysOfMonth = 0 ; // 某月的天數 private int dayOfWeek = 0 ; // 具體某一天是星期幾 private int lastDaysOfMonth = 0 ; // 上一個月的總天數 private Context context; private String[] dayNumber = new String[ 42 ]; // 一個gridview中的日期存入此數組中 private SpecialCalendar sc = null ; private String currentYear = "" ; private String currentMonth = "" ; private int currentFlag = - 1 ; // 用于標記當天 private String showYear = "" ; // 用于在頭部顯示的年份 private String showMonth = "" ; // 用于在頭部顯示的月份 private String animalsYear = "" ; private String leapMonth = "" ; // 閏哪一個月 // 系統當前時間 private String sysDate = "" ; private String sys_year = "" ; private String sys_month = "" ; private String sys_day = "" ; private boolean flag; // 標記是不是本月 private String[] data; private int a; public MyCalendarAdapter() { Date date = new Date(); sysDate = sdf.format(date); // 當期日期 sys_year = sysDate.split( "-" )[ 0 ]; sys_month = sysDate.split( "-" )[ 1 ]; sys_day = sysDate.split( "-" )[ 2 ]; } public MyCalendarAdapter(Context context, int year_c, int month_c, int day_c, String[] a) { // TODO Auto-generated constructor stub this (); this .context = context; sc = new SpecialCalendar(); currentYear = String.valueOf(year_c); // 得到當前的年份 currentMonth = String.valueOf(month_c); getCalendar(Integer.parseInt(currentYear), Integer.parseInt(currentMonth)); data = a; } @Override public int getCount() { return dayNumber.length; } @Override public Object getItem( int position) { return position; } @Override public long getItemId( int position) { return position; } @Override public View getView( int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView== null ){ holder= new ViewHolder(); convertView= LayoutInflater.from(context).inflate(R.layout.item_sign_data, null ); holder.iv_bg= (ImageView) convertView.findViewById(R.id.iv_bg); holder.tv_date= (TextView) convertView.findViewById(R.id.tv_date); convertView.setTag(holder); } else { holder= (ViewHolder) convertView.getTag(); } String d = dayNumber[position]; holder.tv_date.setText(d); holder.tv_date.setTextColor(Color.GRAY); if (position < daysOfMonth + dayOfWeek && position >= dayOfWeek) { // 當前月信息顯示 holder.tv_date.setTextColor(Color.parseColor( "#2a2a2a" )); // 當月字體設黑 flag = true ; a++; } else { flag = false ; } if (flag) { if (a <= data.length) { String att = data[a - 1 ]; if (att.equals( "1" )) { //簽到 holder.tv_date.setTextColor(Color.parseColor( "#2a2a2a" )); holder.iv_bg.setVisibility(View.VISIBLE); } } } if (currentFlag == position) { // 設置當天的背景 // String dv="今日"; // SpannableString sp = new SpannableString(d + "\n" + dv);//當天字體加粗 // sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, // d.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // sp.setSpan(new RelativeSizeSpan(1.2f), 0, d.length(), // Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // sp.setSpan(new RelativeSizeSpan(0.75f), d.length() , // dayNumber[position].length()+ 3, // Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // holder.tv_date.setText(sp); // holder.iv_bg.setVisibility(View.VISIBLE); holder.iv_bg.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_sign_bg_today)); } return convertView; } class ViewHolder{ private ImageView iv_bg; private TextView tv_date; } // 得到某年的某月的天數且這月的第一天是星期幾 public void getCalendar( int year, int month) { isLeapyear = sc.isLeapYear(year); // 是否為閏年 daysOfMonth = sc.getDaysOfMonth(isLeapyear, month); // 某月的總天數 dayOfWeek = sc.getWeekdayOfMonth(year, month); // 某月第一天為星期幾 lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1 ); // 上一個月的總天數 getweek(year, month); } // 將一個月中的每一天的值添加入數組dayNuber中 private void getweek( int year, int month) { int j = 1 ; int flag = 0 ; String lunarDay = "" ; // 得到當前月的所有日程日期(這些日期需要標記) for ( int i = 0 ; i < dayNumber.length; i++) { if (i < dayOfWeek) { // 前一個月 int temp = lastDaysOfMonth - dayOfWeek + 1 ; dayNumber[i] = (temp + i) + "" ; } else if (i < daysOfMonth + dayOfWeek) { // 本月 String day = String.valueOf(i - dayOfWeek + 1 ); // 得到的日期 dayNumber[i] = i - dayOfWeek + 1 + "" ; // 對于當前月才去標記當前日期 if (sys_year.equals(String.valueOf(year)) && sys_month.equals(String.valueOf(month)) && sys_day.equals(day)) { // 標記當前日期 currentFlag = i; } setShowYear(String.valueOf(year)); setShowMonth(String.valueOf(month)); } else { // 下一個月 dayNumber[i] = j + "" ; j++; } } String abc = "" ; for ( int i = 0 ; i < dayNumber.length; i++) { abc = abc + dayNumber[i] + ":" ; } } public String getShowYear() { return showYear; } public void setShowYear(String showYear) { this .showYear = showYear; } public String getShowMonth() { return showMonth; } public void setShowMonth(String showMonth) { this .showMonth = showMonth; } public String getAnimalsYear() { return animalsYear; } public void setAnimalsYear(String animalsYear) { this .animalsYear = animalsYear; } public String getLeapMonth() { return leapMonth; } public void setLeapMonth(String leapMonth) { this .leapMonth = leapMonth; } } |
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
|
public class SpecialCalendar { private int daysOfMonth = 0 ; // 某月的天數 private int dayOfWeek = 0 ; // 具體某一天是星期幾 // 判斷是否為閏年 public boolean isLeapYear( int year) { if (year % 100 == 0 && year % 400 == 0 ) { return true ; } else if (year % 100 != 0 && year % 4 == 0 ) { return true ; } return false ; } // 得到某月有多少天數 public int getDaysOfMonth( boolean isLeapyear, int month) { switch (month) { case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 : daysOfMonth = 31 ; break ; case 4 : case 6 : case 9 : case 11 : daysOfMonth = 30 ; break ; case 2 : if (isLeapyear) { daysOfMonth = 29 ; } else { daysOfMonth = 28 ; } } return daysOfMonth; } // 指定某年中的某月的第一天是星期幾 public int getWeekdayOfMonth( int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(year, month - 1 , 1 ); dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1 ; return dayOfWeek; } } |
積分當然是交給后臺處理返回了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/dailog/article/details/52343907