接着上一篇文章继续。
1.@EActivity(R.id.xxx):表示这个类是一个activity并且对应某个布局文件
- @EActivity(R.layout.activity_main)
- public class MainActivity extends Activity {
- }
- 可以看到,我们没有了onCreate方法,也没有了setContentView方法,依然能够运行出界面
-
2.@ViewById(R.id.xxx):相当于findViewById(R.id.xxx)
-
- @EActivity(R.layout.activity_main)
- public class MainActivity extends Activity {
- @ViewById(R.id.btn)
- public Button btn;
- }
-
3.@Click(R.id.xxx):注解一个方法上,表示这个方法是某个按钮的点击事件
-
- @EActivity(R.layout.activity_main)
- public class MainActivity extends Activity {
- @ViewById(R.id.btn)
- public Button btn;
- @Click(R.id.btn)
- void myClick(){
- Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();
- }
- }
-
- 还可以对多个按钮注解点击事件
- @Click({R.id.button1,R.id.button2,R.id.button3})
- void buttonClicked(Button btn){
- switch(btn.getId()){
- case R.id.button1:
- //这里实现button1的点击事件
- break;
- ...
- }
- }
-
4.@AfterViews:只有所有View都注入完毕之后,才会执行该注解的方法,一般进行一些初始化的操作
- @EActivity(R.layout.activity_main)
- public class MainActivity extends Activity {
- @ViewById(R.id.btn)
- public Button btn;
- @Click(R.id.btn)
- void myClick(){
- Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();
- }
- @AfterViews
- void init(){
- btn.setText("按钮注入完毕后才执行此方法");
- }
- }
-
-
5.@Extrs(String params):获得其它地方跳转过来时所传递过来的数据,括号中的字符串即为传递过来的数据的key
-
注:跳转的时候类名后要加下划线
- Intent intent = new Intent(MainActivity.this,SecondActivity_.class);
- Bundle bundle = new Bundle();
- bundle.putString("hello","hello");
- intent.putExtras(bundle);
- startActivity(intent);
-
-
- @Extra("hello")
- String params;
-
6.@Background和@UiThread
-
这两个通常结合用,前一个用来写后台方法,后一个专注于UI控件的操作
@Background作用:相当于用来进行后台操作的方法(比如请求网络资源,操作文件等等),被Backgroung注解的方法里面不能进行任何UI操作,否则会报错。 -
- @Background
- void httprequest(){
- //进行http操作
- //操作数据库
- //操作文件
- }
-
-
- @UiThread
- void runinUI(){
- Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();
- }
-
7.@EFragment:用来标识某个类属于fragment并指定到布局文件
-
-
- @EFragment(R.layout.layout_myfragment)
- public class MyFragment extends Fragment{
- }
-
8.@FragmentArg:用来给当前Fragment接收别的地方传来的数据
-
给myfragment传递一个参数
-
- @EActivity(R.layout.activity_main)
- public class MainActivity extends Activity {
- @ViewById(R.id.btn)
- public Button btn;
- @Click(R.id.btn)
- void myClick(){
- MyFragment fragment = new MyFragment_();
- Bundle bundle = new Bundle();
- bundle.putString("hello","...");
- //设置传递的参数
- fragment.setArguments(bundle);
- FragmentTransaction transaction = getFragmentManager().beginTransaction();
- transaction.add(R.id.content, fragment);
- transaction.commit();
- }
- }
- myfragment接收一个hello的参数
-
-
- @EFragment(R.layout.layout_myfragment)
- public class MyFragment extends Fragment{
- @FragmentArg("hello")
- String params;
- @AfterViews
- void init(){
- Toast.makeText(getActivity(), params, Toast.LENGTH_SHORT).show();
- }
- }
<string name="app_name">My Application</string>
10.@EBean可以用来注解类
@EBean
public
class
MyClass {
}
也可以用来注解接口括号中必须为该接口下确定的子类
@Bean
(MyImplementation.
class
)
MyInterface myInterface;
11.在普通类中注入跟环境:@RootContext用来注入当前类的一个服务,列入需要的context、service等
@EBean
public
class
MyClass {
@RootContext
Context context;
// Only injected if the root context is an activity
@RootContext
Activity activity;
// Only injected if the root context is a service
@RootContext
Service service;
// Only injected if the root context is an instance of MyActivity
@RootContext
MyActivity myActivity;
}
|
- 12.@afterinject该注解将会在该类创建初期做一些操作
@AfterInject
public
void
doSomethingAfterInjection() {
// notificationManager and dependency are set
}
-
- 13.若该类是一个单例模式,则可以如下声明为单例
-
@EBean
(scope = Scope.Singleton)
public
class
MySingleton {
}
-
-
-
-
- 更多注解用法大全链接:http://www.2cto.com/kf/201408/327955.html
- @EActivity(R.layout.activity_main)
- public class MainActivity extends Activity {
- }
- 可以看到,我们没有了onCreate方法,也没有了setContentView方法,依然能够运行出界面