@EActivity
原来在没用(Android
Annotations)之前,我们
写代码是这样写:
class="java">
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
}
不过有了AndroidAnnotations之后,我们可以这样写,使用@EActivity
注解
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_test);
// }
}
当然,上面的
注释就不用写了
不过@EActivity注解,你还有修改“AndroidManifest.xml”,在TestActivity类名后,加个“_”
<activity
android:name=".TestActivity_"
android:label="@string/title_activity_test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
@EApplication
@EApplication
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
}
}
当然了“AndroidManifest.xml”这个文件还是要修改的,同样也是要加“_”
<application
android:name=".MyApplication_" >
</application>
@App
没有这个注解我们要这么写代码:
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
private MyApplication_ app;
TextView tvTest = null;
@AfterViews
void afterView(){
tvTest = (TextView)this.findViewById(R.id.tv_test);
app = (MyApplication_) getApplication();
tvTest.setText(app.getValue());
}
}
用了@App这个注解后,我们可以这样写:
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
TextView tvTest = null;
@App
MyApplication app;
@AfterViews
void afterView(){
tvTest = (TextView)this.findViewById(R.id.tv_test);
tvTest.setText(app.getValue());
}
}
@ViewById
这个注解可能是我们用到的最常用一个了,每次我们写的
最多的代码都是this.findViewById,这个注解可以帮助我们省去不少的工作。
如前一个
例子
public class TestActivity extends Activity {
TextView tvTest = null;
@App
MyApplication app;
@AfterViews
void afterView(){
tvTest = (TextView)this.findViewById(R.id.tv_test);
tvTest.setText(app.getValue());
}
}
现在我们的代码也可以这么写:
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
@App
MyApplication app;
@ViewById(R.id.tv_test)
TextView tvTest;
@AfterViews
void afterView(){
tvTest.setText(app.getValue());
}
}
当然了如果你的变量名与R.id.tv_test的名称一致,你也可以这下写,这样我们又省去了不少的代码量:
@ViewById(R.id.tv_test)
TextView tvTest;
@ViewById
TextView tv_test;