标签:android style class blog c code
尊重原创 http://write.blog.csdn.net/postedit/26507351
这篇文章主要讲解注解实现findViewById的功能,首先我们来熟悉一下在java中怎么定义一个注解和解析一个注解
注解的概念是在jdk5.0中提出来的,在java.lang的包中已经定义了三个注解:Override,Deprecated,SuppressWarnings注解的定义和接口的接口非常像,在interface的前面多了一个@
public @interface TestPerson
{
}
public @interface TestPerson
{
//name既是这个注解的属性,也是注解的方法,调用name()返回值就是name
String name() default "gavin";
}
//用来标注某个类是用来干嘛的
public @interface ClassFunction
{
String value() default "";
}
//用来标注类中的方法是被谁测试的
public @interface TestPerson
{
//name是属性而不是方法,gavin是它的默认值,在定义的时候可以不用给定默认值
String name() default "gavin";
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}那么我们就完善我们自己的注解吧
@Target(ElementType.METHOD)//作用于方法
@Retention(RetentionPolicy.RUNTIME)//在运行时有效(即运行时保留)
public @interface TestPerson
{
//name是属性而不是方法,gavin是它的默认值,在定义的时候可以不用给定默认值
String name() default "gavin";
}
@Target(ElementType.TYPE)//作用于类上
@Retention(RetentionPolicy.RUNTIME)//在运行时有效(即运行时保留)
public @interface ClassFunction
{
String value() default "";
}
@ClassFunction("用于描述一个人的基本信息")
public class Person
{
private static final String TAG = "Person";
@TestPerson(name="jj")
public void setName()
{
}
}
/**
* com.annotation.TestPerson
* @author yuanzeyao <br/>
* create at 2014年5月21日 下午1:30:14
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestPerson {
String name();
}
public class Person {
private static final String TAG = "Person";
@TestPerson(name="gavin")
public void getName()
{
}
}
public class Main {
private static final String TAG = "Main";
public static void main(String[] args)
{
Person person=new Person();
//获得Person对应的Class
Class<Person> clazz=Person.class;
try {
//找到getName方法
Method method=clazz.getMethod("getName",null);
//判断是否被TestPerson标注
if(method.isAnnotationPresent(TestPerson.class))
{
//调用getName方法
method.invoke(person, null);
//得到TestPerson注解的实例
TestPerson test=method.getAnnotation(TestPerson.class);
//获得其name属性
String name=test.name();
System.out.println("this method is test by-->"+name);
}
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectView
{
//id就是控件id,在某一个控件上使用注解标注其id
int id() default -1;
}
public class MainActivity extends Activity
{
public static final String TAG="MainActivity";
//标注TextView的id
@InjectView(id=R.id.tv_img)
private TextView mText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
autoInjectAllField(this);
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
}
if(mText!=null)
mText.setText("Hello Gavin");
}
public void autoInjectAllField(Activity activity) throws IllegalAccessException, IllegalArgumentException
{
//得到Activity对应的Class
Class clazz=this.getClass();
//得到该Activity的所有字段
Field []fields=clazz.getDeclaredFields();
Log.v(TAG, "fields size-->"+fields.length);
for(Field field :fields)
{
//判断字段是否标注InjectView
if(field.isAnnotationPresent(InjectView.class))
{
Log.v(TAG, "is injectView");
//如果标注了,就获得它的id
InjectView inject=field.getAnnotation(InjectView.class);
int id=inject.id();
Log.v(TAG, "id--->"+id);
if(id>0)
{
//反射访问私有成员,必须加上这句
field.setAccessible(true);
//然后对这个属性复制
field.set(activity, activity.findViewById(id));
}
}
}
}
}Android中通过注解代替findViewById方法,布布扣,bubuko.com
标签:android style class blog c code
原文地址:http://blog.csdn.net/yuanzeyao/article/details/26507351