androidannotations中的注解大全

     接着上一篇文章继续。

1.@EActivity(R.id.xxx):表示这个类是一个activity并且对应某个布局文件


  1. @EActivity(R.layout.activity_main)  
  2. public class MainActivity extends Activity {  
  3.   
  4. 可以看到,我们没有了onCreate方法,也没有了setContentView方法,依然能够运行出界面

  1. 2.@ViewById(R.id.xxx):相当于findViewById(R.id.xxx)

    1. @EActivity(R.layout.activity_main)  
    2. public class MainActivity extends Activity {  
    3.   
    4.     @ViewById(R.id.btn)  
    5.     public Button btn;  
    6.   
    7. }  

  2. 3.@Click(R.id.xxx):注解一个方法上,表示这个方法是某个按钮的点击事件


    1. @EActivity(R.layout.activity_main)  
    2. public class MainActivity extends Activity {  
    3.   
    4.     @ViewById(R.id.btn)  
    5.     public Button btn;  
    6.   
    7.     @Click(R.id.btn)  
    8.     void myClick(){  
    9.         Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();  
    10.     }  
    11.   
    12. }  
      1. 还可以对多个按钮注解点击事件
      1. @Click({R.id.button1,R.id.button2,R.id.button3})    
      2. void buttonClicked(Button btn){    
      3.     switch(btn.getId()){    
      4.     case R.id.button1:    
      5.     //这里实现button1的点击事件    
      6.         break;    
      7.         ...    
      8.     }    
      9. }   

    13. 4.@AfterViews:只有所有View都注入完毕之后,才会执行该注解的方法,一般进行一些初始化的操作


      1. @EActivity(R.layout.activity_main)  
      2. public class MainActivity extends Activity {  
      3.   
      4.     @ViewById(R.id.btn)  
      5.     public Button btn;  
      6.   
      7.     @Click(R.id.btn)  
      8.     void myClick(){  
      9.         Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();  
      10.     }  
      11.   
      12.     @AfterViews  
      13.     void init(){  
      14.         btn.setText("按钮注入完毕后才执行此方法");  
      15.     }  
      16.   
      17. }  

    14. 5.@Extrs(String params):获得其它地方跳转过来时所传递过来的数据,括号中的字符串即为传递过来的数据的key

    15. 注:跳转的时候类名后要加下划线
      1. Intent intent = new Intent(MainActivity.this,SecondActivity_.class);  
      2. Bundle bundle = new Bundle();  
      3. bundle.putString("hello","hello");  
      4. intent.putExtras(bundle);  
      5. startActivity(intent);  

        1.  @Extra("hello")  
        2.     String params;  


    16. 6.@Background和@UiThread

    17. 这两个通常结合用,前一个用来写后台方法,后一个专注于UI控件的操作
      @Background作用:相当于用来进行后台操作的方法(比如请求网络资源,操作文件等等),被Backgroung注解的方法里面不能进行任何UI操作,否则会报错。
      1. @Background  
      2. void httprequest(){  
      3.      //进行http操作  
      4.      //操作数据库  
      5.      //操作文件  
      6. }  

      @UiThread作用:相当于用来进行UI控件的操作,被UiThread注解的方法里面不能进行任何后台操作,否则会报错
      1. @UiThread  
      2. void runinUI(){  
      3.      Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();  
      4. }  

    18. 7.@EFragment:用来标识某个类属于fragment并指定到布局文件


      1. @EFragment(R.layout.layout_myfragment)  
      2. public class MyFragment extends Fragment{  
      3.   
      4.  
    1. 8.@FragmentArg:用来给当前Fragment接收别的地方传来的数据

    2. 给myfragment传递一个参数
      1. @EActivity(R.layout.activity_main)  
      2. public class MainActivity extends Activity {  
      3.   
      4.     @ViewById(R.id.btn)  
      5.     public Button btn;  
      6.   
      7.     @Click(R.id.btn)  
      8.     void myClick(){  
      9.         MyFragment fragment = new MyFragment_();  
      10.         Bundle bundle = new Bundle();  
      11.         bundle.putString("hello","...");  
      12.     //设置传递的参数  
      13.         fragment.setArguments(bundle);  
      14.         FragmentTransaction transaction = getFragmentManager().beginTransaction();  
      15.         transaction.add(R.id.content, fragment);  
      16.         transaction.commit();  
      17.     }  
      18. }
      19. myfragment接收一个hello的参数

        1. @EFragment(R.layout.layout_myfragment)  
        2. public class MyFragment extends Fragment{  
        3.   
        4.   
        5.     @FragmentArg("hello")  
        6.     String params;  
        7.   
        8.     @AfterViews  
        9.     void init(){  
        10.         Toast.makeText(getActivity(), params, Toast.LENGTH_SHORT).show();  
        11.     }  
        12.   
        13. }  

      9.@StringRes(R.string.xxx):资源文件注解,相当于在strings.xml文件中设置了
           <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;
 
}

  1. 12.@afterinject该注解将会在该类创建初期做一些操作
    @AfterInject
       public void doSomethingAfterInjection() {
         // notificationManager and dependency are set
       }

  2. 13.若该类是一个单例模式,则可以如下声明为单例
  3. @EBean (scope = Scope.Singleton)
    public class MySingleton {
     
    }




  4. 更多注解用法大全链接:http://www.2cto.com/kf/201408/327955.html


  1. @EActivity(R.layout.activity_main)  
  2. public class MainActivity extends Activity {  
  3.   
  4. 可以看到,我们没有了onCreate方法,也没有了setContentView方法,依然能够运行出界面
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值