-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuiBhelp.java
220 lines (199 loc) · 8.38 KB
/
GuiBhelp.java
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package utils;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import Annotations.DoSomething_logic;
import Annotations.NoNull;
import Annotations.ViewSet;
import Annotations.setContext;
import Annotations.viewonclick;
import Interface_s.Context_IF;
import Interface_s.setContext_IF;
public class GuiBhelp {
public static void setcontext(Class classaction,Object classobj,Activity context){
try {
Field activity_IF=classaction.getDeclaredField("activity_IF");
activity_IF.setAccessible(true);
activity_IF.set(classobj,context);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
try {
Field activity_IF=classaction.getSuperclass().getDeclaredField("activity_IF");
activity_IF.setAccessible(true);
activity_IF.set(classobj,context);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static void setcontext(Class classaction,Object classobj,View context){
try {
Field view_set=classaction.getSuperclass().getDeclaredField("view_IF");
view_set.setAccessible(true);
view_set.set(classobj,context);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
try {
Field view_set=classaction.getSuperclass().getDeclaredField("view_IF");
view_set.setAccessible(true);
view_set.set(classobj,context);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static void bind(Class classaction, Activity context, Object classobj){//设置view
Field[] ms = classaction.getDeclaredFields();
if (classaction.isAnnotationPresent(setContext.class)){
setcontext(classaction,classobj,context);
}else if(classaction.getSuperclass().isAnnotationPresent(setContext.class)){
setcontext(classaction,classobj,context);
}
for (Field m : ms) {
if (m.isAnnotationPresent(ViewSet.class)) {
ViewSet d1=m.getAnnotation(ViewSet.class);
m.setAccessible(true);
try {
if(d1.isiv()){
Glide.with(context).load(m.get(classobj)).into((ImageView) context.findViewById(d1.id()));
}else{
((TextView)context.findViewById(d1.id())).setText(d1.head()+m.get(classobj)+d1.foot());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
public static void bind(Class classaction, View view, Object classobj){//设置view
Field[] ms = classaction.getDeclaredFields();
if (classaction.isAnnotationPresent(setContext.class)){
setcontext(classaction,classobj,view);
}else if(classaction.getSuperclass().isAnnotationPresent(setContext.class)){
setcontext(classaction,classobj,view);
}
for (Field m : ms) {
//Log.e("查找shezhi注解","ss");
if (m.isAnnotationPresent(ViewSet.class)) {
ViewSet d1=m.getAnnotation(ViewSet.class);
//Log.e("设置注解","ss");
m.setAccessible(true);
if (d1.isiv()) {
try {
Glide.with(view.getContext()).load(m.get(classobj)).into((ImageView) view.findViewById(d1.id()));
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
((TextView)view.findViewById(d1.id())).setText(d1.head()+m.get(classobj)+d1.foot());
} catch (Exception e) {
//Log.e("设置注解","ss"+e.getMessage());
e.printStackTrace();
}
}
}
}
}
public static void setview(Class classaction, View view, Object classobj){//设置点击事件 以及 view的逻辑操作
Method[] ms = classaction.getDeclaredMethods();
if (classaction.isAnnotationPresent(setContext.class)){
setcontext(classaction,classobj,view);
}else if(classaction.getSuperclass().isAnnotationPresent(setContext.class)){
setcontext(classaction,classobj,view);
}
for (Method m : ms) {
//Log.e("查找点击注解","ss");
if(m.isAnnotationPresent(viewonclick.class)){
//Log.e("点击注解","ss");
viewonclick d1=m.getAnnotation(viewonclick.class);
m.setAccessible(true);
view.findViewById(d1.value()).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Log.e("点击了","ss");
try {
m.invoke(classobj);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
});
}else if(m.isAnnotationPresent(DoSomething_logic.class)){
DoSomething_logic d1=m.getAnnotation(DoSomething_logic.class);
m.setAccessible(true);
try {
m.invoke(classobj);
} catch (IllegalAccessException | InvocationTargetException ex) {
try {
m.invoke(classobj,view);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
ex.printStackTrace();
}
}
}
}
public static boolean testnull(Object m, NoNull d1,int pos){
Class<?> targetClass = m.getClass();
try {
if (targetClass.equals(EditText.class)) {
if(((EditText)m).getText()==null || ((EditText)m).getText().length()==0){
ToastUtils.showShort(d1.value()[pos]);
return true;
}else {
return false;
}
}else {
if(m.toString().length()==0){
ToastUtils.showShort(d1.value()[pos]);
return true;
}else {
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public static boolean notnull(Class classaction, Object classobj){//非空 判断 toast
Field[] ms = classaction.getDeclaredFields();
for (Field m : ms) {
if (m.isAnnotationPresent(NoNull.class)) {
NoNull d1=m.getAnnotation(NoNull.class);
m.setAccessible(true);
try {
//Log.e("field type","=="+m.getType());
if(m.getType().equals(List.class)){
for (int i = 0; i < ((List) m.get(classobj)).size(); i++) {
if (testnull(((List) m.get(classobj)).get(i),d1,i)) {
return false;
}
}
}else{
if (testnull(m.get(classobj),d1,0)) {
return false;
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
ToastUtils.showShort(e.getMessage());
return false;
}
}
}
return true;
}
}