@Tyhj
        
        2020-12-04T08:37:25.000000Z
        字数 3254
        阅读 1470
    Android
支持aspectj和jitpack,在工程的grade添加
...dependencies {classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4'}...//Add it in your root build.gradle at the end of repositories:allprojects {repositories {...maven { url 'https://jitpack.io' }}}
在使用的module的grade中添加依赖和注解处理器,以及支持aspectj
...apply plugin: 'android-aspectjx'...implementation 'com.github.tyhjh.Annotation:annotationlibrary:v1.1.3'annotationProcessor 'com.github.tyhjh.Annotation:annotator:v1.1.3'
需要支持lambda 表达式,在模块的build.gradle的android节点下面添加支持
compileOptions {sourceCompatibility = '1.8'targetCompatibility = '1.8'}
需要使用注解的类中需要初始化
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//注册,进行控件的初始化CatAnnotation.injectView(this);
该注解用于控件的初始化,可以传入控件的ID,当控件的ID和变量名一致是可以不传入
//变量名和控件ID值一样@ViewByIdTextView tvLogin,tvName;
被注解的变量名可以和控件ID值不一样,但是需要手动设置控件ID值,这种方法只能在app模块使用
//手动设置id@ViewById(R.id.et_pwd)EditText etPwd;
该注解为设置控件的点击事件,同样可以传入控件ID,改方法不需要初始化控件,可以直接使用
@Clickvoid tvLogin() {// TODO call server...}@Clickvoid tvLogin(R.id.tvLogin) {// TODO call server...}
新增防抖动功能,使用了@Click注解的按钮默认在300ms内只能点击一次,可以通过设置全局修改,也可以修改单个点击事件的间隔
//全局修改点击间隔,需要尽早设置AvoidShake.setClickIntervalTime(1000);//单个设置点击间隔@Click(interval = 100)void txtView() {...}//不使用注解的控件设置点击间隔txtView.setOnClickListener(new AvoidShakeClickHelper(500, new AvoidShakeListener() {@Overridepublic void onClick(View v) {//todo}}));
该注解为方法切换到子线程运行,可以进行延迟执行,方法不能有返回值,否则不生效,delay值默认为0
@Background(delay = 1000)void backgroud() {Log.e("backgroud", Thread.currentThread().getName() + ":" + System.currentTimeMillis());}
该注解为方法切换到主线程运行,可以进行延迟执行,方法不能有返回值,否则不生效,delay值默认为0
@UiThread(delay = 1000 * 5)void toast(String msg) {Log.e("UiThread", Thread.currentThread().getName() + ":" + System.currentTimeMillis());}
该注解为RecyclerView滑动到底部监听,可以实现下拉加载更多的功能,使用很方便,变量名就是方法名,也可以手动设置控件ID值;可以设置pageSize的值,如果当前加载的item数量小于pageSize那么就不会触发方法,默认滑动到底部监听就会触发方法
@RecyclerMore(pageSize = 5)void ryclView() {mList.addAll(mList2);mAdapter.notifyDataSetChanged();}
该注解为CheckBox的OnCheckedChange监听方法,被注解的方法需要有下面两个参数
@CheckBoxChangevoid swTest(boolean isChecked, CompoundButton swTest) {Toast.makeText(this, "isChecked:" + swTest.isChecked(), Toast.LENGTH_SHORT).show();}
该注解为获取被注解的方法执行的时间
//对方法进行注解@ExecuteTimeprivate void etTest() {Toast.makeText(MainActivity.this, "哈哈哈", Toast.LENGTH_SHORT).show();}//全局注册时间监听ExecuteManager.getInstance().setPrinter((executeTime, annotion, methodInfo) -> {Log.i(TAG,"方法耗时为: "+executeTime);Log.i(TAG,"方法的详情:"+methodInfo.toString());});
该注解为自定义注解,用于监听方法执行使用,可以用于数据埋点啥的;
//对方法进行注解@CustomAnnotationprivate void etTest() {Toast.makeText(MainActivity.this, "哈哈哈", Toast.LENGTH_SHORT).show();}//对方法执行进行监听ExecuteManager.getInstance().addExecuteListener(new IExecuteListener() {@Overridepublic void before(CustomAnnotation annotation, MethodInfo methodInfo) {//一个方法准备开始执行Log.i(TAG,"before MethodInfo is "+methodInfo.toString());}@Overridepublic void after(CustomAnnotation annotation, MethodInfo methodInfo) {//一个方法执行完成Log.i(TAG,"after MethodInfo is "+methodInfo.toString());}});
- 使用注解实现Android线程切换:https://www.jianshu.com/p/89d7b88eb76c
 - Android编译时注解:https://www.jianshu.com/p/3052fa51ee95
 - Android中注解的使用:https://www.jianshu.com/p/de13b00042d6