最近接触到了一个注解式快速开发开源框架,叫androidannotations,github地址为https://github.com/excilys/androidannotations/wiki,有兴趣的朋友可以去看看。
相信大家在写Android程序的时候,会经常使用到findViewById的操作,如果View的个数少的话还好,要是很多的话,对我们而言,就会感到很枯燥,重复的findViewById,也很耗时间,那么这种时候,androidannotations便能解决这个问题,让findViewById的操作变得很简单、很简洁。
这里要说一下的是,解决findViewById的操作只是其中一个很小的功能,androidannotations的强大之处远不止这些,它有非常多的注解式标签,主要分为以下几个部分:
这里只做简单的说明,详细的请到github上查看,下面讲一下AndroidStudio配置方法:
androidannotations-api-3.0.1.jar复制到libs目录;
修改build.gradle,如下:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.0.1'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.danger.Activity"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName "com.danger.Activity"
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
apt "org.androidannotations:androidannotations:$AAVersion"
}
}
下面,说一下某些常用的注解式标签的使用:
- 在Activity类之前加上@EActivity标签,用于指定Activtiy对应的布局:
@EActivity(R.layout.activity_main)
- 为声明的View之前加上@ViewById标签:
@ViewById
TextView textView1;
@ViewById(R.id.textView2)
TextView textView2;
这里注意一下:
@ViewById 该标签用于替换findViewById的操作,但有一点需要注意,不能将view声明为private
如果声明的属性变量的名字与id相同,则不需要在ViewById中指定id
这样,在Activity中声明的View就与xml中对应的View绑定起来了,下面给出我的示例代码(其中只包含了很少的标签):
/**
* 用注解的方法是不能用private修饰的
*/
@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
/**
* @ViewById 该标签用于替换findViewById的操作,但有一点需要注意,不能将view声明为private
* 如果声明的属性变量的名字与id相同,则不需要在ViewById中指定id
*/
@ViewById
Button button;
@ViewById
Button thrid_button;
@ViewById
Button background_button;
@ViewById
TextView textView1;
@ViewById(R.id.textView2)
TextView textView2;
@ViewById(R.id.textView3)
TextView textView3;
/**
* @StringRes(R.string.name) 该标签用于获取资源文件中的数据
*/
@StringRes(R.string.name)
String name;
/**
* @StringArrayRes(R.array.name_array) 该标签用于获取资源文件中的array
*/
@StringArrayRes(R.array.name_array)
String[] name_array;
/**
* @DimensionRes(R.dimen.fontSize) 该标签用于获取资源文件中的dimension值
*/
@DimensionRes(R.dimen.fontSize)
float fontSize;
public static final String NAME = "name";
public static final String AGE = "age";
/**
* 三种初始化方法;
* 1.通过@ViewById(R.id.textView),此处id与变量名不需一致
* 2.通过@ViewById,此处id与变量名需一致
* 3.通过ViewsById:
*
* @ViewsById({R.id.textView1,R.id.textView2}) List<TextView> list;
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("danger", "Main Thread id = " + Thread.currentThread().getId());
}
/**
* @Click(R.id.button) 该标签用于为view添加点击事件,前置条件是需要用@ViewById初始化
* 也可通过集合的方式为View添加点击事件
* @Click({R.id.textView1,R.id.textView2})
*/
@Click(R.id.button)
public void startActivity() {
Intent intent = new Intent(MainActivity.this, SecondActivity_.class);
intent.putExtra(NAME, "danger");
intent.putExtra(AGE, "17");
startActivity(intent);
}
@Click(R.id.thrid_button)
public void toThridActivity() {
Intent intent = new Intent(MainActivity.this, ThridActivity_.class);
startActivity(intent);
}
/**
* @Background 该标签将该方法放入后台线程中执行
*/
@Background
public void doSomething() {
Log.i("danger", "Thread id = " + Thread.currentThread().getId());
updateUI();
}
/**
* @UiThread 与handler进行交互,在后台线程中将更新UI的操作递交给handler完成
* 在一个方法中前加上@UiThread 标签,则该方法会被放入主线程中执行
*/
@UiThread
public void updateUI() {
textView3.setText("I have been updated!!!");
}
@Click(R.id.background_button)
public void backgroundClick() {
doSomething();
}
/**
* @AfterViews 该标签用于在view初始化(findViewById)之后调用
*/
@AfterViews
public void setTextView() {
textView1.setText("TextView1");
textView2.setText("TextView2");
textView3.setText(name);
textView3.setTextSize(fontSize);
}
}