Android Annotation的一些简单使用
来源:互联网 发布:新加坡国家顶级域名 编辑:程序博客网 时间:2024/05/20 04:31
1. 配置
根据github上github可以知道annotation(后面简称AA)怎么在as项目中配置的。
因为我的as中gradle版本是2.2的,还不能使用2.3中内置的插件,所以还是要配置apt 在根gradle中将复制进去
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.0' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}
接下来的和github上它提示的不一样,github上是说将如下代码也复制进根gradle中
repositories { mavenCentral() mavenLocal()}apply plugin: 'com.android.application'apply plugin: 'android-apt'def AAVersion = 'XXX'dependencies { apt "org.androidannotations:androidannotations:$AAVersion" compile "org.androidannotations:androidannotations-api:$AAVersion"}apt { arguments { // you should set your package name here if you are using different application IDs // resourcePackageName "your.package.name" // You can set optional annotation processing options here, like these commented options: // logLevel 'INFO' // logFile '/var/log/aa.log' }}
但是其实并不用,这样做的好处是多个modul时都要使用AA,则只要在这里配置一次就行了,但是实际项目中这样的情况还是少,一般都是一个主modul,就算有其他modul,也是库用来依赖的。并且在modul中的gradle中配置可以少一些代码。所以我接下来直接去modul下的gradle中,将如下代码复制进去
apply plugin: 'android-apt'def AAVersion = '4.2.0'
apply plugin: ‘com.android.application’这行代码并不需要,因为已经有了,同理如下,也有了不需要
android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { minSdkVersion 9 targetSdkVersion 23 }}
所以我们只要将下面这代码复制到dependencies中就可以了
apt "org.androidannotations:androidannotations:$AAVersion" compile "org.androidannotations:androidannotations-api:$AAVersion"
下面这行代码也复制进去,不知道有什么用,看字面上意思,看不懂
apt { arguments { // you should set your package name here if you are using different application IDs // resourcePackageName "your.package.name" // You can set optional annotation processing options here, like these commented options: // logLevel 'INFO' // logFile '/var/log/aa.log' }}
2.干货
我使用了下并且看了下wiki,AA给我的感觉就是鸡肋,AA可以注入android中的很多,不仅仅这些简单的findviewbyid,或者setonclicklistener,还有activity,application,bean(实体类),service,还有事件的注入,其实setOnClickListener其实就是,还有其他的等下代码说。但是不知道是不是我理解不够,只学了两三天,感觉很多东西就是强心注入,并不能减轻或者简化使用,等下看例子就知道了。当然widgit的注入还是不错的。
A.widgit的注入
比如button,textview等等这些。xml代码如下,主要注意id
<Button android:id="@+id/bt1" android:text="点击跳转" android:layout_width="match_parent" android:layout_height="50dp" />
java代码如下
@ViewById(R.id.bt1) Button button1;
其实这里面很有讲究,上面这行代码其实就已经获取到Button的实例了,我们可以操作button1了。比如我们想拿到这个button的内容,直接在onCreate中写上button1.getText().toString();,如果我们在使用@ViewById时并不想(R.id.bt1)也可以,只要xml中id是这个Button的命名就行,这种思路在AA中很多地方都用到了,所以上面的代码其实也可以这样写
@ViewById Button bt1;
其他的textview或者editetext之类的都是一样的。
B.事件的注入
我这里举两个例子,一个使点击事件的注入,这个使用的还算正常,还有个是内容变化事件的注入,太蛋疼了。
点击事件,就拿上面那个button举例,我们可以这样些
@Click(R.id.bt1) void clickButtion1(){ //点击后想做的事情 }
同样因为XML中button的id原因,也可以这样写
@Click void bt1(){ //点击后想做的事情 }
这样子还算正常,确实会简单些,但是如果是复杂的事件注入,方法里面很多参数的,并且这些参数在方法中需要使用的,就尴尬了,如EditText,
<EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="50dp" />
EditText有个监听内容变化的事件,正常写应该是这样的
@ViewById EditText et1; et1.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { } });
如果我们采用AA注入写,则就是这样,还需要手动将参数复制到方法中,最主要我们并不知道里面有哪些参数…..有点麻烦。
@BeforeTextChange(R.id.et1) void beforeEd(CharSequence s, int start, int count, int after){ } @TextChange(R.id.et1) void textChange(CharSequence s, int start, int before, int count){ } @AfterTextChange(R.id.et1) void afterChanger(Editable s){ }
C.Activity、Application的注入
首先来看activity的注入,我们必须要知道一点,activity只要一注入,则manifest中一定要使用对应的下划线activity注册,并且其他地方使用时也使用下划线的activity。如下面使用@EActivity
@EActivity(R.layout.activity_main)public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }}
清单文件中应该是MainActivity_,见下面
<activity android:name=".MainActivity_"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
这样子写,清单文件中可能会报错,这还好,但是在java代码中如果哪里牵扯这注入的这个activity,则也使用activity_,也会提示错误,很正常,因为没有这个类,但是运行则没事,代码可以跑起来。比如刚才上面的button,点击后跳转一个注入的activity,
新建SecondActivity,并将它注入
@EActivity(R.layout.activity_second)public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); }}
清单文件中
<activity android:name=".SecondActivity_" />
接下来在click中,看这个secondactivity,使用的是带下划线的
@Click(R.id.bt1) void clickButtion1(){ Intent intent = new Intent(this,SecondActivity_.class); startActivity(intent); }
写上去后显示报错的,但是运行后就没事了。
application注入,比如我新建一个MyApplication,并将它使用AA注入,并且在里面写一个方法,就是显示一个弹框
@EApplicationpublic class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); } public void showToast(){ Toast.makeText(getApplicationContext(),"我是application里面的方法",Toast.LENGTH_SHORT).show(); }}
和activity一样,在清单文件中也需要加下划线。
android:name=".MyApplication_"
在java代码中其他地方使用这个MyApplication时采用@App就可以获取到它实例,比如我在刚才的secondActivity中加一个button,点击它调用MyApplication里面的那个方法。
@App MyApplication myApplication; //如果application已经注入, 使用@App就能获取它的实例
@Click(R.id.bt2) void clickButton(){ myApplication.showToast(); }
ok,其他的比如service只要需要在清单文件中配置,只要注入了,都需要使用它对应带下划线的…..
D.bean实体类的注入
这个就简单了,其实就是使用@EBean对bean注入,在需要使用的地方使用@Bean获取它的实例,唯一要注意的就是需要在这个bean中写一个空构造函数。这其实很好理解我们在使用@Bean获取实例时应该也是使用构造函数获取对象的,所以需要个构造函数,如下
@EBeanpublic class Person { private String name; private String age; public Person() { }}
@Bean Person person;
3.最后
因为github上AA的内容实在太多了没看完,步骤,但是根据上面这些在项目中已经可以减少很多重复代码了 。
- Android Annotation的一些简单使用
- android annotation配置及简单使用
- 简单的使用jackson的annotation
- 简单的Annotation框架的使用
- 最简单的android annotation demo
- Spring IoC Annotation 的简单使用
- Annotation的一些命令
- Android Annotation注解的剖析和使用
- Android中Annotation的使用详解
- Hibernate 简单的annotation
- Annotation的简单例子
- AS使用Android Annotation
- Android annotation 使用注意事项
- 初步了解jackson,简单的使用jackson的annotation
- 初步了解jackson,简单的使用jackson的annotation
- android annotation快速简单入门
- Android中adb工具的一些简单使用
- spring中aop的简单使用(Annotation方式)
- Face Detection with the Faster R-CNN(数据集标注对比研究报告 )
- 计算机图形学 OPENGL学习
- 页面显示空格(nbsp)的兼容问题
- Glide:调整图片大小
- 黄庭坚的《清明》
- Android Annotation的一些简单使用
- Android性能优化典范
- 层次遍历
- JMeter常用函数整理
- Android DownloadManger下载管理
- 数据库隔离级别
- Qt Widgets Application,开发的扫雷游戏
- 多维数组排序
- Layout_weight的含义