简介
AndroidAnnotations是一个开源框架,通过使用它开放出来的注解api,可以大大的减少无关痛痒的代码量,简洁代码。
项目包名可在AndroidManifest.xml中的package确认。 在gradle/目录下的build.gradle文件(全局gradle)中添加下面红色粗体字配置: 常用的有@EActivity、@EFragment、@EService等,进行注解了的组件才可使用其他注解功能。 简单的控件绑定,资源文件中的id与控件名一致即可不在注解后加上括号及对应控件的id,@Extra也是。其他地方需要声明控件id的皆同理。 当View相关的成员变量初始化完毕后,会调用拥有@AfterViews注解的方法,可以在里面初始化一些界面控件等。 还有@TextChange、@ItemClick、@SeekBarProgressChange等。 UI线程执行的方法加个@UiThread,异步线程方法加个@Background,两者的交互就是方法直接的相互调用,不用再使用Handler去发送接收Message了。 注册广播接收,简单搞定,不需要其他操作。相比下传统的方式: 瞬间简洁了很多吧 直接使用@SharedPref可以简单地使用SharedPreferences的功能。 首先,建一个类存放需要存取的数据: 括号后面的是默认值,接下来就是简单的使用了,首先在用到的类里声明: 使用起来特别方便,需要特别说明的是,这些数据要在一些不同的组件中同步共享,需在@SharedPref加上(value=SharedPref.Scope.UNIQUE),之前在activity和service中的数据总是对不上,找了好久才找到原因。 想要在普通的类中也用上注解,只需在类名加上@EBean 使用时,声明: 有一些要注意的是: @EBean注解的类,只能有一个构造方法,且这个构造方法必须无参数或者只有context参数。 在activity等组件内声明了后,不用再去new这个类,否则会出错。 比较常用的一些方法及说明大概就是这些,当然Annotation还有不少东西,想要了解得更深入可以到文首的链接处查看官方的使用说明,进一步了解! 本文链接:http://www.codeceo.com/article/android-annotation-usage.htmlbuildscript{
repositories{
jcenter()
}
dependencies{// replace with the current version of the Android pluginclasspath'com.android.tools.build1.5.0'// replace with the current version of the android-apt pluginclasspath'com.neenbedankt.gradle.plugins:android-apt:1.4+'}
}
allprojects{
repositories{
jcenter()
}
}
taskclean(type:Delete){
deleterootProject.buildDir
}
常用注解
组件注解
@EActivity(R.layout.acitvity_main)public class MainActivity extends Activity{
...
}
资源引用的注解
@ViewById(R.id.tv_title)//此处可去掉括号部分
TextView tv_title;
@ViewById
ImageView img_menu;
@ViewById
RelativeLayout rl_light;
@Extra
String mTitle;
@StringRes(R.string.hello)
String hello;
事件绑定
@Click
void img_back() {
finish(); overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
}
比较方便的一些注解
异步线程与UI线程
@UiThreadvoid doSomething(){
...
}@Backgroundvoid doSomething(){
...
}
广播接收
@Receiver(actions = Utils.ACTION_BLE_DISCONNETED) public void bleDisconnect() {
...
} @Receiver(actions = Utils.ACTION_UPDATE_WATER_SHOW) public void updateWaterShow(@Receiver.Extra(Utils.VALUE_ADDRESS) long water) { if (switchIsOpen)
edt_water.setText(water + "");
}
Private final BroadcastReceiver mGattUpdateReceiver = newBroadcastReceiver(){ @Override
public void onReceive(Contextcontext,Intentintent){ final Stringaction=intent.getAction(); if(Stringaction.equal(Utils.ACTION_STOP_SCAN)){
...
}
}
};private IntentFilter makeGattUpdateIntentFilter() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Utils.ACTION_STOP_SCAN);return intentFilter;
}
registerReceiver(mGattUpdateReceiver,makeGattUpdateIntentFilter());
unregisterReceiver(mGattUpdateReceiver);
SharedPreferences
@SharedPref(value=SharedPref.Scope.UNIQUE)public interface MyPrefs { @DefaultBoolean(true) boolean isFirstIn(); @DefaultString("") String ignoreVersion(); @DefaultInt(0) int shockLevel();
}
@Pref
MyPrefs_ myPrefs; boolean isFirstIn = myPrefs.isFirstIn().get();
myPrefs.isFirstIn().put(false);
@EBen
@EBeanpublic class MyClass { @UiThread
void updateUI() {
}
@EActivitypublic class MyActivity extends Activity { @Bean
MyClass myClass;
}
总结
本文作者:码农网 – 苏耀东
共同學習,寫下你的評論
評論加載中...
作者其他優質文章