前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >androidannotions开发框架在Eclipse中的配置

androidannotions开发框架在Eclipse中的配置

作者头像
泥豆芽儿 MT
发布2018-09-11 11:05:27
1.3K0
发布2018-09-11 11:05:27
举报
文章被收录于专栏:木头编程 - moTzxx

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1333564

1.首先前去下载androidannotions所对应的jar包

网址:https://github.com/excilys/androidannotations/wiki/Download

2.对Eclipse中所选中的项目进行jar包的配置,可以新建一个文件夹放置,截图如下:

3.右键单击项目,选择属性,并进行后面的勾选,导入jar包

4.对所应用的Activity或其他类,在配置文件AndroidManifest.xml中,进行后缀的下划线添加

5.最后clearn一下项目,进行后续的代码开发

@EActivity

 示例:

代码语言:javascript
复制
@EActivity(R.layout.main)
public class MyActivity extends Activity {

}

@fragment

示例:

代码语言:javascript
复制
代码语言:javascript
复制
@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
}

注册:

代码语言:javascript
复制
<fragment
        android:id="@+id/myFragment"
        android:name="com.company.MyFragment_"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

创建:

代码语言:javascript
复制
MyFragment fragment = new MyFragment_();

普通类:

代码语言:javascript
复制
@EBean
public class MyClass {

}

注意:这个类必须仅仅只能有一个构造函数,参数最多有一个context。

Activity中使用:

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @Bean
  MyOtherClass myOtherClass;

}

也可以用来声明接口:

代码语言:javascript
复制
@Bean(MyImplementation.class)
    MyInterface myInterface;

在普通类中还可以注入根环境:

代码语言:javascript
复制
@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;

}

如果想在类创建时期做一些操作可以:

代码语言:javascript
复制
@AfterInject
  public void doSomethingAfterInjection() {
    // notificationManager and dependency are set
  }

单例类需要如下声明:

代码语言:javascript
复制
@EBean(scope = Scope.Singleton)
public class MySingleton {

}

注意:在单例类里面不可以注入view和事件绑定,因为单例的生命周期比Activity和Service的要长,以免发生内存溢出。

@EView

代码语言:javascript
复制
@EView
public class CustomButton extends Button {

        @App
        MyApplication application;

        @StringRes
        String someStringResource;

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

注册:

代码语言:javascript
复制
<com.androidannotations.view.CustomButton_
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

创建:

代码语言:javascript
复制
CustomButton button = CustomButton_.build(context);

@EViewGroup

代码语言:javascript
复制
@EViewGroup(R.layout.title_with_subtitle)
public class TitleWithSubtitle extends RelativeLayout {

    @ViewById
    protected TextView title, subtitle;

    public TitleWithSubtitle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setTexts(String titleText, String subTitleText) {
        title.setText(titleText);
        subtitle.setText(subTitleText);
    }

}

注册:

代码语言:javascript
复制
<com.androidannotations.viewgroup.TitleWithSubtitle_
        android:id="@+id/firstTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

@EApplication

代码语言:javascript
复制
@EApplication
public class MyApplication extends Application {

}

Activity中使用:

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @App
  MyApplication application;

}

@EService

代码语言:javascript
复制
@EService
public class MyService extends Service {

}

跳转service:

代码语言:javascript
复制
MyService_.intent(getApplication()).start();

停止service:

代码语言:javascript
复制
MyService_.intent(getApplication()).stop();

@EReceiver

代码语言:javascript
复制
@EReceiver
public class MyReceiver extends BroadcastReceiver {

}

@Receiver

可以替代声明BroadcastReceiver

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @Receiver(actions = "org.androidannotations.ACTION_1")
  protected void onAction1() {

  }

}

@EProvider

代码语言:javascript
复制
@EProvider
public class MyContentProvider extends ContentProvider {

}

@ViewById

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  // Injects R.id.myEditText,变量名称必须和布局的id名称一致
  @ViewById
  EditText myEditText;

  @ViewById(R.id.myTextView)
  TextView textView;
}

@AfterViews

代码语言:javascript
复制
@EActivity(R.layout.main)
public class MyActivity extends Activity {

    @ViewById
    TextView myTextView;

    @AfterViews
    void updateTextWithDate() {
代码语言:javascript
复制
//一定要在这里进行view的一些设置,不要在oncreate()中设置,因为oncreate()在执行时 view还没有注入
代码语言:txt
复制
    myTextView**.**setText**(**"Date: " **+** **new** **Date());** **}[...]** 

@StringRes

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @StringRes(R.string.hello)
  String myHelloString;//不能设置成私有变量

  @StringRes
  String hello;

}

@ColorRes

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @ColorRes(R.color.backgroundColor)
  int someColor;

  @ColorRes
  int backgroundColor;

}

@AnimationRes

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @AnimationRes(R.anim.fadein)
  XmlResourceParser xmlResAnim;

  @AnimationRes
  Animation fadein;

}

@DimensionRes

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @DimensionRes(R.dimen.fontsize)
  float fontSizeDimension;

  @DimensionRes
  float fontsize;

}

@DImensionPixelOffsetRes

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @DimensionPixelOffsetRes(R.string.fontsize)
  int fontSizeDimension;

  @DimensionPixelOffsetRes
  int fontsize;

}

@DimensionPixelSizeRes

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @DimensionPixelSizeRes(R.string.fontsize)
  int fontSizeDimension;

  @DimensionPixelSizeRes
  int fontsize;

}

其他的Res:

  • @BooleanRes
  • @ColorStateListRes
  • @DrawableRes
  • @IntArrayRes
  • @IntegerRes
  • @LayoutRes
  • @MovieRes
  • @TextRes
  • @TextArrayRes
  • @StringArrayRes

@Extra

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @Extra("myStringExtra")
  String myMessage;

  @Extra("myDateExtra")
  Date myDateExtraWithDefaultValue = new Date();

}

或者:

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  // The name of the extra will be "myMessage",名字必须一致
  @Extra
  String myMessage;
}

传值:

代码语言:javascript
复制
MyActivity_.intent().myMessage("hello").start() ;

@SystemService

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {//
  @SystemService
  NotificationManager notificationManager;

}

@HtmlRes

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  // Injects R.string.hello_html
  @HtmlRes(R.string.hello_html)
  Spanned myHelloString;

  // Also injects R.string.hello_html
  @HtmlRes
  CharSequence helloHtml;

}

@FromHtml

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {//必须用在TextView

  @ViewById(R.id.my_text_view)
  @FromHtml(R.string.hello_html)
  TextView textView;

  // Injects R.string.hello_html into the R.id.hello_html view
  @ViewById
  @FromHtml
  TextView helloHtml;

}

@NonConfigurationInstance

代码语言:javascript
复制
public class MyActivity extends Activity {//等同于 Activity.onRetainNonConfigurationInstance()

  @NonConfigurationInstance
  Bitmap someBitmap;

  @NonConfigurationInstance
  @Bean
  MyBackgroundTask myBackgroundTask;

}

@HttpsClient

代码语言:javascript
复制
代码语言:javascript
复制
@HttpsClient
HttpClient httpsClient;

示例:

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

    @HttpsClient(trustStore=R.raw.cacerts,
        trustStorePwd="changeit", 
        hostnameVerif=true)
    HttpClient httpsClient;

    @AfterInject
    @Background
    public void securedRequest() {
        try {
            HttpGet httpget = new HttpGet("https://www.verisign.com/");
            HttpResponse response = httpsClient.execute(httpget);
            doSomethingWithResponse(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @UiThread
    public void doSomethingWithResponse(HttpResponse resp) {
        Toast.makeText(this, "HTTP status " + resp.getStatusLine().getStatusCode(), Toast.LENGTH_LONG).show();
    }
}

@FragmentArg

代码语言:javascript
复制
@EFragment
public class MyFragment extends Fragment {//等同于 Fragment Argument

  @FragmentArg("myStringArgument")
  String myMessage;

  @FragmentArg
  String anotherStringArgument;

  @FragmentArg("myDateExtra")
  Date myDateArgumentWithDefaultValue = new Date();

}
代码语言:javascript
复制
MyFragment myFragment = MyFragment_.builder()
  .myMessage("Hello")
  .anotherStringArgument("World")
  .build();

@Click

代码语言:javascript
复制
@Click(R.id.myButton)
void myButtonWasClicked() {
    [...]
}
@Click
void anotherButton() {//如果不指定则函数名和id对应
    [...]
}
@Click
void yetAnotherButton(View clickedView) {
    [...]
}

其他点击事件:

  • Clicks with @Click
  • Long clicks with @LongClick
  • Touches with @Touch

AdapterViewEvents 

  • Item clicks with @ItemClick
  • Long item clicks with @ItemLongClick
  • Item selection with @ItemSelect

有两种方式调用:

1.

代码语言:javascript
复制
@EActivity(R.layout.my_list)
public class MyListActivity extends Activity {

    // ...

    @ItemClick
    public void myListItemClicked(MyItem clickedItem) {//MyItem是adapter的实体类,等同于adapter.getItem(position)

    }

    @ItemLongClick
    public void myListItemLongClicked(MyItem clickedItem) {

    }

    @ItemSelect
    public void myListItemSelected(boolean selected, MyItem selectedItem) {

    }

}

2.

代码语言:javascript
复制
@EActivity(R.layout.my_list)
public class MyListActivity extends Activity {

    // ...

    @ItemClick
    public void myListItemClicked(int position) {//位置id

    }

    @ItemLongClick
    public void myListItemLongClicked(int position) {

    }

    @ItemSelect
    public void myListItemSelected(boolean selected, int position) {

    }

}

@SeekBarProgressChange

代码语言:javascript
复制
代码语言:javascript
复制
//等同于SeekBar.OnSeekBarChangeListener.onProgressChanged(SeekBar, int, boolean)
代码语言:javascript
复制
@SeekBarProgressChange(R.id.seekBar)
 void onProgressChangeOnSeekBar(SeekBar seekBar, int progress, boolean fromUser) {
    // Something Here
 }

 @SeekBarProgressChange(R.id.seekBar)
 void onProgressChangeOnSeekBar(SeekBar seekBar, int progress) {
    // Something Here
 }

 @SeekBarProgressChange({R.id.seekBar1, R.id.seekBar2})
 void onProgressChangeOnSeekBar(SeekBar seekBar) {
    // Something Here
 }

 @SeekBarProgressChange({R.id.seekBar1, R.id.seekBar2})
 void onProgressChangeOnSeekBar() {
    // Something Here
 }@SeekBarTouchStart and @SeekBarTouchStop

@SeekBarTouchStart 和 @SeekBarTouchStop

接受开始和结束事件的监听

@TextChange

代码语言:javascript
复制
@TextChange(R.id.helloTextView)
 void onTextChangesOnHelloTextView(CharSequence text, TextView hello, int before, int start, int count) {
    // Something Here
 }

 @TextChange
 void helloTextViewTextChanged(TextView hello) {
    // Something Here
 }

 @TextChange({R.id.editText, R.id.helloTextView})
 void onTextChangesOnSomeTextViews(TextView tv, CharSequence text) {
    // Something Here
 }

 @TextChange(R.id.helloTextView)
 void onTextChangesOnHelloTextView() {
    // Something Here
 }

@BeforeTextChange

代码语言:javascript
复制
@BeforeTextChange(R.id.helloTextView)
 void beforeTextChangedOnHelloTextView(TextView hello, CharSequence text, int start, int count, int after) {
    // Something Here
 }

 @BeforeTextChange
 void helloTextViewBeforeTextChanged(TextView hello) {
    // Something Here
 }

 @BeforeTextChange({R.id.editText, R.id.helloTextView})
 void beforeTextChangedOnSomeTextViews(TextView tv, CharSequence text) {
    // Something Here
 }

 @BeforeTextChange(R.id.helloTextView)
 void beforeTextChangedOnHelloTextView() {
    // Something Here
 }

@AfterTextChange

代码语言:javascript
复制
@AfterTextChange(R.id.helloTextView)
 void afterTextChangedOnHelloTextView(Editable text, TextView hello) {
    // Something Here
 }

 @AfterTextChange
 void helloTextViewAfterTextChanged(TextView hello) {
    // Something Here
 }

 @AfterTextChange({R.id.editText, R.id.helloTextView})
 void afterTextChangedOnSomeTextViews(TextView tv, Editable text) {
    // Something Here
 }

 @AfterTextChange(R.id.helloTextView)
 void afterTextChangedOnHelloTextView() {
    // Something Here
 }

@OptionsMenu和OptionsItem

代码语言:javascript
复制
@EActivity
@OptionsMenu(R.menu.my_menu)
public class MyActivity extends Activity {

    @OptionMenuItem
    MenuItem menuSearch;

    @OptionsItem(R.id.menuShare)
        void myMethod() {
          // You can specify the ID in the annotation, or use the naming convention
        }

    @OptionsItem
    void homeSelected() {
      // home was selected in the action bar
          // The "Selected" keyword is optional
    }

    @OptionsItem
    boolean menuSearch() {
          menuSearch.setVisible(false);
          // menuSearch was selected
          // the return type may be void or boolean (false to allow normal menu processing to proceed, true to consume it here)
          return true;
    }

    @OptionsItem({ R.id.menu_search, R.id.menu_delete })
    void multipleMenuItems() {
      // You can specify multiple menu item IDs in @OptionsItem
    }

    @OptionsItem
    void menu_add(MenuItem item) {
      // You can add a MenuItem parameter to access it
    }
}

或者:

代码语言:javascript
复制
@EActivity
@OptionsMenu({R.menu.my_menu1, R.menu.my_menu2})
public class MyActivity extends Activity {

}

@Background

执行:

代码语言:javascript
复制
void myMethod() {
    someBackgroundWork("hello", 42);
}

@Background
void someBackgroundWork(String aParam, long anotherParam) {
    [...]
}

取消:

代码语言:javascript
复制
void myMethod() {
    someCancellableBackground("hello", 42);
    [...]
    boolean mayInterruptIfRunning = true;
    BackgroundExecutor.cancelAll("cancellable_task", mayInterruptIfRunning);
}

@Background(id="cancellable_task")
void someCancellableBackground(String aParam, long anotherParam) {
    [...]
}

非并发执行:

代码语言:javascript
复制
void myMethod() {
    for (int i = 0; i < 10; i++)
        someSequentialBackgroundMethod(i);
}

@Background(serial = "test")
void someSequentialBackgroundMethod(int i) {
    SystemClock.sleep(new Random().nextInt(2000)+1000);
    Log.d("AA", "value : " + i);
}

延迟:

代码语言:javascript
复制
@Background(delay=2000)
void doInBackgroundAfterTwoSeconds() {
}

@UiThread

UI线程:

代码语言:javascript
复制
void myMethod() {
    doInUiThread("hello", 42);
}

@UiThread
void doInUiThread(String aParam, long anotherParam) {
    [...]
}

延迟:

代码语言:javascript
复制
@UiThread(delay=2000)
void doInUiThreadAfterTwoSeconds() {
}

优化UI线程:

代码语言:javascript
复制
@UiThread(propagation = Propagation.REUSE)
void runInSameThreadIfOnUiThread() {
}

进度值改变:

代码语言:javascript
复制
@EActivity
public class MyActivity extends Activity {

  @Background
  void doSomeStuffInBackground() {
    publishProgress(0);
    // Do some stuff
    publishProgress(10);
    // Do some stuff
    publishProgress(100);
  }

  @UiThread
  void publishProgress(int progress) {
    // Update progress views
  }

}

@OnActivityResult

代码语言:javascript
复制
@OnActivityResult(REQUEST_CODE)
 void onResult(int resultCode, Intent data) {
 }

 @OnActivityResult(REQUEST_CODE)
 void onResult(int resultCode) {
 }

 @OnActivityResult(ANOTHER_REQUEST_CODE)
 void onResult(Intent data) {
 }

 @OnActivityResult(ANOTHER_REQUEST_CODE)
 void onResult() {
 }

以上的注释用法基本包含了平常程序中的事件绑定,用AndroidAnnotations框架可以专注于做逻辑开发,最主要是简化代码编写,容易维护。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年12月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • AdapterViewEvents 
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档