Skip to content

Commit 3506c2e

Browse files
committed
加入@ContentView注解,用于Activity,见sample。
1 parent 99c15cb commit 3506c2e

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

library/src/com/lidroid/xutils/ViewUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.lidroid.xutils.util.LogUtils;
2424
import com.lidroid.xutils.util.core.DoubleKeyValueMap;
2525
import com.lidroid.xutils.view.*;
26+
import com.lidroid.xutils.view.annotation.ContentView;
2627
import com.lidroid.xutils.view.annotation.PreferenceInject;
2728
import com.lidroid.xutils.view.annotation.ResInject;
2829
import com.lidroid.xutils.view.annotation.ViewInject;
@@ -87,8 +88,18 @@ public static void inject(Object handler, PreferenceActivity preferenceActivity)
8788
@SuppressWarnings("ConstantConditions")
8889
private static void injectObject(Object handler, ViewFinder finder) {
8990

91+
Class<?> handlerType = handler.getClass();
92+
93+
// inject ContentView
94+
if (Activity.class.isAssignableFrom(handlerType)) {
95+
ContentView contentView = handlerType.getAnnotation(ContentView.class);
96+
if (contentView != null) {
97+
((Activity) handler).setContentView(contentView.value());
98+
}
99+
}
100+
90101
// inject view
91-
Field[] fields = handler.getClass().getDeclaredFields();
102+
Field[] fields = handlerType.getDeclaredFields();
92103
if (fields != null && fields.length > 0) {
93104
for (Field field : fields) {
94105
ViewInject viewInject = field.getAnnotation(ViewInject.class);
@@ -134,7 +145,7 @@ private static void injectObject(Object handler, ViewFinder finder) {
134145
}
135146

136147
// inject event
137-
Method[] methods = handler.getClass().getDeclaredMethods();
148+
Method[] methods = handlerType.getDeclaredMethods();
138149
if (methods != null && methods.length > 0) {
139150
String eventName = OnClick.class.getCanonicalName();
140151
String prefix = eventName.substring(0, eventName.lastIndexOf('.'));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2013. wyouflf ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.lidroid.xutils.view.annotation;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
@Target(ElementType.TYPE)
24+
@Retention(RetentionPolicy.RUNTIME)
25+
public @interface ContentView {
26+
int value();
27+
}

sample/src/com/lidroid/xutils/sample/MyActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import com.lidroid.xutils.sample.fragment.DbFragment;
1414
import com.lidroid.xutils.sample.fragment.HttpFragment;
1515
import com.lidroid.xutils.util.LogUtils;
16+
import com.lidroid.xutils.view.annotation.ContentView;
1617
import com.lidroid.xutils.view.annotation.ViewInject;
1718

19+
@ContentView(R.layout.main)
1820
public class MyActivity extends FragmentActivity {
1921

2022
@ViewInject(R.id.tabhost)
@@ -36,7 +38,6 @@ public class MyActivity extends FragmentActivity {
3638
@Override
3739
protected void onCreate(Bundle savedInstanceState) {
3840
super.onCreate(savedInstanceState);
39-
setContentView(R.layout.main);
4041

4142
LogUtils.customTagPrefix = "xUtilsSample"; // 方便调试时过滤 adb logcat 输出
4243
LogUtils.allowI = false; //关闭 LogUtils.i(...) 的 adb log 输出

0 commit comments

Comments
 (0)