Android面试相关Android开发android杂

Eventbus3代码分析(三):注解简单使用

2016-10-01  本文已影响91人  dodo_lihao

注解简单使用

这里目的是为了理解, 和简单的使用
这块理解了,后面都是差不多的


简单理解和使用

自己每次在onCreate中写setContentView(R.layout.xxx)
感觉比较麻烦
自己想把注解写到类的前面,这样比较好修改


先定义 @interface AhView
大体代码为:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AhView1 {
    int intR();
}

这里ElementType.TYPE, 上面也有提到过
是表示类或者接口的


InjectHelper1
再写一个帮助类,静态方法便于调用:

public class InjectHelper1 {
    public static int injectObject(Object handler) throws Exception {
        Class<?> handlerType = handler.getClass();
        // inject ContentView
        AhView1 contentView = handlerType.getAnnotation(AhView1.class);
        if (contentView != null) {
            try {
                return contentView.intR();
            } catch (Throwable e) {
                throw new Exception("No injection layout");
            }
        }else{
            throw new Exception("No injection layout");
        }
    }
}

大体意思,就是


BaseActivity1
写父类 BaseActivity1,
在父类的onCreate中,setContentView(InjectHelper1.injectObject(this));

public class BaseActivity1 extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle arg0) {
            super.onCreate(arg0);            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            try {
                setContentView(InjectHelper1.injectObject(this));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}

具体Activity测试
具体的代码


这里直接在类前面配置值, 拿到值后,BaseActivity中直接赋值

最后可以得到:



简单理解和使用2

上面有给对应的Class类配置对应的值
但是,配置的时候,需要设置对应的方法名字
例如:
@AhView1(intR = R.layout.activity_main)
我们也可以不用写对应的配置值(当操作比较多的时候,设置值是必要的,如果只有一个值,可以用下面办法去省略)

和上面的类似


AhView
这里我们只需要把方法,设置成value即可

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AhView {
    int value();
}

InjectHelper
方式相同

public class InjectHelper {
    public static int injectObject(Object handler) throws Exception {
        Class<?> handlerType = handler.getClass();
        // inject ContentView
        AhView contentView = handlerType.getAnnotation(AhView.class);
        if (contentView != null) {
            try {
                return contentView.value();
            } catch (Throwable e) {
                throw new RuntimeException("No injection layout");
            }
        }else{
            throw new RuntimeException("No injection layout");
        }
    }
}

BaseActivity
和上面类似

public class BaseActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle arg0) {
            super.onCreate(arg0);            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            try {
                setContentView(InjectHelper.injectObject(this));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}

MainActivity
这个时候,对应的设置,就不用 【方法名 = xxx】了

@AhView(R.layout.activity_main)
public class MainActivity extends BaseActivity {

}

最后也能看到对应的界面


简单总结

这里只是用这里小例子,理解对应的注解 和 反射 的过程
对应的方式很多
这里只是和大家一起熟悉流程


具体测试代码

简单的代码,存放在
https://github.com/2954722256/use_little_demo
中的annotation_test对应Module
供简单参考


下一篇我们可以了解Eventbus3代码分析(四):@interface Subscribe分析

上一篇 下一篇

猜你喜欢

热点阅读