[关闭]
@Tyhj 2020-12-04T08:37:25.000000Z 字数 3254 阅读 1159

集成Android常用注解

Android


集成方法

支持aspectj和jitpack,在工程的grade添加

  1. ...
  2. dependencies {
  3. classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4'
  4. }
  5. ...
  6. //Add it in your root build.gradle at the end of repositories:
  7. allprojects {
  8. repositories {
  9. ...
  10. maven { url 'https://jitpack.io' }
  11. }
  12. }

在使用的module的grade中添加依赖和注解处理器,以及支持aspectj

  1. ...
  2. apply plugin: 'android-aspectjx'
  3. ...
  4. implementation 'com.github.tyhjh.Annotation:annotationlibrary:v1.1.3'
  5. annotationProcessor 'com.github.tyhjh.Annotation:annotator:v1.1.3'

需要支持lambda 表达式,在模块的build.gradle的android节点下面添加支持

  1. compileOptions {
  2. sourceCompatibility = '1.8'
  3. targetCompatibility = '1.8'
  4. }

使用方法

控件的依赖注入

初始化

需要使用注解的类中需要初始化

  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.activity_main);
  4. //注册,进行控件的初始化
  5. CatAnnotation.injectView(this);

@ViewById

该注解用于控件的初始化,可以传入控件的ID,当控件的ID和变量名一致是可以不传入

  1. //变量名和控件ID值一样
  2. @ViewById
  3. TextView tvLogin,tvName;

被注解的变量名可以和控件ID值不一样,但是需要手动设置控件ID值,这种方法只能在app模块使用

  1. //手动设置id
  2. @ViewById(R.id.et_pwd)
  3. EditText etPwd;

@Click

该注解为设置控件的点击事件,同样可以传入控件ID,改方法不需要初始化控件,可以直接使用

  1. @Click
  2. void tvLogin() {
  3. // TODO call server...
  4. }
  5. @Click
  6. void tvLogin(R.id.tvLogin) {
  7. // TODO call server...
  8. }
点击防抖动

新增防抖动功能,使用了@Click注解的按钮默认在300ms内只能点击一次,可以通过设置全局修改,也可以修改单个点击事件的间隔

  1. //全局修改点击间隔,需要尽早设置
  2. AvoidShake.setClickIntervalTime(1000);
  3. //单个设置点击间隔
  4. @Click(interval = 100)
  5. void txtView() {
  6. ...
  7. }
  8. //不使用注解的控件设置点击间隔
  9. txtView.setOnClickListener(new AvoidShakeClickHelper(500, new AvoidShakeListener() {
  10. @Override
  11. public void onClick(View v) {
  12. //todo
  13. }
  14. }));

@Background

该注解为方法切换到子线程运行,可以进行延迟执行,方法不能有返回值,否则不生效,delay值默认为0

  1. @Background(delay = 1000)
  2. void backgroud() {
  3. Log.e("backgroud", Thread.currentThread().getName() + ":" + System.currentTimeMillis());
  4. }

@UiThread

该注解为方法切换到主线程运行,可以进行延迟执行,方法不能有返回值,否则不生效,delay值默认为0

  1. @UiThread(delay = 1000 * 5)
  2. void toast(String msg) {
  3. Log.e("UiThread", Thread.currentThread().getName() + ":" + System.currentTimeMillis());
  4. }

@RecyclerMore

该注解为RecyclerView滑动到底部监听,可以实现下拉加载更多的功能,使用很方便,变量名就是方法名,也可以手动设置控件ID值;可以设置pageSize的值,如果当前加载的item数量小于pageSize那么就不会触发方法,默认滑动到底部监听就会触发方法

  1. @RecyclerMore(pageSize = 5)
  2. void ryclView() {
  3. mList.addAll(mList2);
  4. mAdapter.notifyDataSetChanged();
  5. }

@CheckBoxChange

该注解为CheckBox的OnCheckedChange监听方法,被注解的方法需要有下面两个参数

  1. @CheckBoxChange
  2. void swTest(boolean isChecked, CompoundButton swTest) {
  3. Toast.makeText(this, "isChecked:" + swTest.isChecked(), Toast.LENGTH_SHORT).show();
  4. }

@ExecuteTime

该注解为获取被注解的方法执行的时间

  1. //对方法进行注解
  2. @ExecuteTime
  3. private void etTest() {
  4. Toast.makeText(MainActivity.this, "哈哈哈", Toast.LENGTH_SHORT).show();
  5. }
  6. //全局注册时间监听
  7. ExecuteManager.getInstance().setPrinter((executeTime, annotion, methodInfo) -> {
  8. Log.i(TAG,"方法耗时为: "+executeTime);
  9. Log.i(TAG,"方法的详情:"+methodInfo.toString());
  10. });

@CustomAnnotation

该注解为自定义注解,用于监听方法执行使用,可以用于数据埋点啥的;

  1. //对方法进行注解
  2. @CustomAnnotation
  3. private void etTest() {
  4. Toast.makeText(MainActivity.this, "哈哈哈", Toast.LENGTH_SHORT).show();
  5. }
  6. //对方法执行进行监听
  7. ExecuteManager.getInstance().addExecuteListener(new IExecuteListener() {
  8. @Override
  9. public void before(CustomAnnotation annotation, MethodInfo methodInfo) {
  10. //一个方法准备开始执行
  11. Log.i(TAG,"before MethodInfo is "+methodInfo.toString());
  12. }
  13. @Override
  14. public void after(CustomAnnotation annotation, MethodInfo methodInfo) {
  15. //一个方法执行完成
  16. Log.i(TAG,"after MethodInfo is "+methodInfo.toString());
  17. }
  18. });

相关推荐文章

项目地址

查看最新版本:https://github.com/tyhjh/Annotation

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注