文章目录

在基类BaseActivity中加入如下函数

1
2
3
4
5
6
7
8
public final <E extends View> E getView (int id) {
try {
return (E) findViewById(id);
} catch (ClassCastException ex) {
Log.e(TAG,"Could not cast View to concrete class.",ex);
throw ex;
}
}

使用ViewFinder类,封装了常用的方法。可以在Activity或View中调用。

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
public class ViewFinder {
private static interface FindWrapper {
View findViewById(int id);
Resources getResources();
}

private static class WindowWrapper implements FindWapper {
private final Window window;

WidowWrapper(final Window window) {
this.window = window;
}

public View findViewById(final int id) {
return window.findViewById(id);
}

public Resources getResources() {
return window.getContext().getResouces();
}
}

private static class ViewWrapper implements FindWrapper {
private final View view;

ViewWrapper(final View view) {
this.view = view;
}

public View findViewById(int id) {
return view.findViewById(id);
}

public Resources getResources() {
return view.getResources();
}
}

private final FindWrapper wrapper;

public ViewFinder(final View view) {
wrapper = new ViewWrapper(view);
}

public ViewFinder(final Window window) {
wrapper = new WindowWrapper(window);
}

public ViewFinder(final Activity activity) {
this(activity.getWindow());
}

public <V extends View> V find (final int id) {
return (V) wrapper.findViewById(id);
}

public ImageView imageView(final int id) {
return find(id);
}

public CompoundButton commpoudButton(final int id) {
return find(id);
}

public TextView textView(final int id) {
return find(id);
}

public TextView setText(final int id, final CharSequence content) {
final TextView text = find(id);
text.setText(content);
return text;
}

public TextView setText(final int id, final int content) {
return setText(id, wrapper.getResources().getString(content));
}

public View onClick(final int id, final Runnable runnable) {
return onClick(id, new OnClickListener() {

public void onClick(View v) {
runnable.run();
}
});
}

public void onClick(final OnClickListener listener, final int... ids) {
for (int id : ids)
find(id).setOnClickListener(listener);
}

public void onClick(final Runnable runnable, final int... ids) {
onClick(new OnClickListener() {

public void onClick(View v) {
runnable.run();
}
}, ids);
}

public ImageView setDrawable(final int id, final int drawable) {
ImageView image = imageView(id);
image.setImageDrawable(image.getResources().getDrawable(drawable));
return image;
}

public CompoundButton onCheck(final int id, final OnCheckedChangeListener listener) {
CompoundButton checkable = find(id);
checkable.setOnCheckedChangeListener(listener);
return checkable;
}

public CompoundButton onCheck(final int id, final Runnable runnable) {
return onCheck(id, new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
runnable.run();
}
});
}

public void onCheck(final OnCheckedChangeListener listener, final int... ids) {
for (int id : ids)
compoundButton(id).setOnCheckedChangeListener(listener);
}

public void onCheck(final Runnable runnable, final int... ids) {
onCheck(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
runnable.run();
}
}, ids);
}
}

文章目录