|
1
2
3
4 |
@EActivity(R.layout.main)public class MyActivity extends Activity {} |
|
1
2
3 |
public class MyFragment extends Fragment {} |
|
1 |
|
|
1 |
MyFragment fragment = new MyFragment_(); |
|
1
2
3
4 |
@EBeanpublic class MyClass {} |
|
1
2
3
4
5
6
7 |
@EActivitypublic class MyActivity extends Activity { @Bean MyOtherClass myOtherClass;} |
|
1
2 |
@Bean(MyImplementation.class) MyInterface myInterface; |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
@EBeanpublic 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
2
3
4 |
@AfterInject public void doSomethingAfterInjection() { // notificationManager and dependency are set } |
|
1
2
3
4 |
@EBean(scope = Scope.Singleton)public class MySingleton {} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
@EViewpublic class CustomButton extends Button { @App MyApplication application; @StringRes String someStringResource; public CustomButton(Context context, AttributeSet attrs) { super(context, attrs); }} |
|
1 |
|
|
1 |
CustomButton button = CustomButton_.build(context); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
@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); }} |
|
1 |
|
|
1
2
3
4 |
@EApplicationpublic class MyApplication extends Application {} |
|
1
2
3
4
5
6
7 |
@EActivitypublic class MyActivity extends Activity { @App MyApplication application;} |
|
1
2
3
4 |
@EServicepublic class MyService extends Service {} |
|
1 |
MyService_.intent(getApplication()).start(); |
|
1 |
MyService_.intent(getApplication()).stop(); |
|
1
2
3
4 |
@EReceiverpublic class MyReceiver extends BroadcastReceiver {} |
|
1
2
3
4
5
6
7
8
9 |
@EActivitypublic class MyActivity extends Activity { @Receiver(actions = "org.androidannotations.ACTION_1") protected void onAction1() { }} |
|
1
2
3
4 |
@EProviderpublic class MyContentProvider extends ContentProvider {} |
|
1
2
3
4
5
6
7
8
9
10 |
@EActivitypublic class MyActivity extends Activity { // Injects R.id.myEditText,变量名称必须和布局的id名称一致 @ViewById EditText myEditText; @ViewById(R.id.myTextView) TextView textView;} |
|
1
2
3
4
5
6
7
8 |
@EActivity(R.layout.main)public class MyActivity extends Activity { @ViewById TextView myTextView; @AfterViews void updateTextWithDate() { |
|
1
2
3
4
5
6
7
8
9
10 |
@EActivitypublic class MyActivity extends Activity { @StringRes(R.string.hello) String myHelloString;//不能设置成私有变量 @StringRes String hello;} |
|
1
2
3
4
5
6
7
8
9
10 |
@EActivitypublic class MyActivity extends Activity { @ColorRes(R.color.backgroundColor) int someColor; @ColorRes int backgroundColor;} |
|
1
2
3
4
5
6
7
8
9
10 |
@EActivitypublic class MyActivity extends Activity { @AnimationRes(R.anim.fadein) XmlResourceParser xmlResAnim; @AnimationRes Animation fadein;} |
|
1
2
3
4
5
6
7
8
9
10 |
@EActivitypublic class MyActivity extends Activity { @DimensionRes(R.dimen.fontsize) float fontSizeDimension; @DimensionRes float fontsize;} |
|
1
2
3
4
5
6
7
8
9
10 |
@EActivitypublic class MyActivity extends Activity { @DimensionPixelOffsetRes(R.string.fontsize) int fontSizeDimension; @DimensionPixelOffsetRes int fontsize;} |
|
1
2
3
4
5
6
7
8
9
10 |
@EActivitypublic class MyActivity extends Activity { @DimensionPixelSizeRes(R.string.fontsize) int fontSizeDimension; @DimensionPixelSizeRes int fontsize;} |
@BooleanRes@ColorStateListRes@DrawableRes@IntArrayRes@IntegerRes@LayoutRes@MovieRes@TextRes@TextArrayRes@StringArrayRes@Extra或者:12345678910@EActivitypublicclassMyActivityextendsActivity {@Extra("myStringExtra")String myMessage;@Extra("myDateExtra")Date myDateExtraWithDefaultValue =newDate();}传值:1234567@EActivitypublicclassMyActivityextendsActivity {// The name of the extra will be "myMessage",名字必须一致@ExtraString myMessage;}@SystemService1MyActivity_.intent().myMessage("hello").start() ;@HtmlRes123456@EActivitypublicclassMyActivityextendsActivity {//@SystemServiceNotificationManager notificationManager;}@FromHtml123456789101112@EActivitypublicclassMyActivityextendsActivity {// Injects R.string.hello_html@HtmlRes(R.string.hello_html)Spanned myHelloString;// Also injects R.string.hello_html@HtmlResCharSequence helloHtml;}@NonConfigurationInstance12345678910111213@EActivitypublicclassMyActivityextendsActivity {//必须用在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@FromHtmlTextView helloHtml;}@HttpsClient12345678910publicclassMyActivityextendsActivity {//等同于 Activity.onRetainNonConfigurationInstance()@NonConfigurationInstanceBitmap someBitmap;@NonConfigurationInstance@BeanMyBackgroundTask myBackgroundTask;}示例:12class="brush:java;">@HttpsClientHttpClient httpsClient;@FragmentArg12345678910111213141516171819202122232425@EActivitypublicclassMyActivityextendsActivity {@HttpsClient(trustStore=R.raw.cacerts,trustStorePwd="changeit",hostnameVerif=true)HttpClient httpsClient;@AfterInject@BackgroundpublicvoidsecuredRequest() {try{HttpResponse response = httpsClient.execute(httpget);doSomethingWithResponse(response);}catch(Exception e) {e.printStackTrace();}}@UiThreadpublicvoiddoSomethingWithResponse(HttpResponse resp) {Toast.makeText(this,"HTTP status "+ resp.getStatusLine().getStatusCode(), Toast.LENGTH_LONG).show();}}12345678910111213@EFragmentpublicclassMyFragmentextendsFragment {//等同于 Fragment Argument@FragmentArg("myStringArgument")String myMessage;@FragmentArgString anotherStringArgument;@FragmentArg("myDateExtra")Date myDateArgumentWithDefaultValue =newDate();}@Click1234MyFragment myFragment = MyFragment_.builder().myMessage("Hello").anotherStringArgument("World").build();其他点击事件:123456789101112@Click(R.id.myButton)voidmyButtonWasClicked() {[...]}@ClickvoidanotherButton() {//如果不指定则函数名和id对应[...]}@ClickvoidyetAnotherButton(View clickedView) {[...]}- Clicks with
@Click - Long clicks with
@LongClick - Touches with
@TouchAdapterViewEvents
- Item clicks with
@ItemClick - Long item clicks with
@ItemLongClick - Item selection with
@ItemSelect有两种方式调用: 1.2.123456789101112131415161718192021@EActivity(R.layout.my_list)publicclassMyListActivityextendsActivity {// ...@ItemClickpublicvoidmyListItemClicked(MyItem clickedItem) {//MyItem是adapter的实体类,等同于adapter.getItem(position)}@ItemLongClickpublicvoidmyListItemLongClicked(MyItem clickedItem) {}@ItemSelectpublicvoidmyListItemSelected(booleanselected, MyItem selectedItem) {}}@SeekBarProgressChange123456789101112131415161718192021@EActivity(R.layout.my_list)publicclassMyListActivityextendsActivity {// ...@ItemClickpublicvoidmyListItemClicked(intposition) {//位置id}@ItemLongClickpublicvoidmyListItemLongClicked(intposition) {}@ItemSelectpublicvoidmyListItemSelected(booleanselected,intposition) {}}1class="brush:java;">//等同于SeekBar.OnSeekBarChangeListener.onProgressChanged(SeekBar, int, boolean)@SeekBarTouchStart 和 @SeekBarTouchStop 接受开始和结束事件的监听 @TextChange12345678910111213141516171819@SeekBarProgressChange(R.id.seekBar)voidonProgressChangeOnSeekBar(SeekBar seekBar,intprogress,booleanfromUser) {// Something Here}@SeekBarProgressChange(R.id.seekBar)voidonProgressChangeOnSeekBar(SeekBar seekBar,intprogress) {// Something Here}@SeekBarProgressChange({R.id.seekBar1, R.id.seekBar2})voidonProgressChangeOnSeekBar(SeekBar seekBar) {// Something Here}@SeekBarProgressChange({R.id.seekBar1, R.id.seekBar2})voidonProgressChangeOnSeekBar() {// Something Here}@SeekBarTouchStartand@SeekBarTouchStop@BeforeTextChange12345678910111213141516171819@TextChange(R.id.helloTextView)voidonTextChangesOnHelloTextView(CharSequence text, TextView hello,intbefore,intstart,intcount) {// Something Here}@TextChangevoidhelloTextViewTextChanged(TextView hello) {// Something Here}@TextChange({R.id.editText, R.id.helloTextView})voidonTextChangesOnSomeTextViews(TextView tv, CharSequence text) {// Something Here}@TextChange(R.id.helloTextView)voidonTextChangesOnHelloTextView() {// Something Here}@AfterTextChange12345678910111213141516171819@BeforeTextChange(R.id.helloTextView)voidbeforeTextChangedOnHelloTextView(TextView hello, CharSequence text,intstart,intcount,intafter) {// Something Here}@BeforeTextChangevoidhelloTextViewBeforeTextChanged(TextView hello) {// Something Here}@BeforeTextChange({R.id.editText, R.id.helloTextView})voidbeforeTextChangedOnSomeTextViews(TextView tv, CharSequence text) {// Something Here}@BeforeTextChange(R.id.helloTextView)voidbeforeTextChangedOnHelloTextView() {// Something Here}@OptionsMenu和OptionsItem12345678910111213141516171819@AfterTextChange(R.id.helloTextView)voidafterTextChangedOnHelloTextView(Editable text, TextView hello) {// Something Here}@AfterTextChangevoidhelloTextViewAfterTextChanged(TextView hello) {// Something Here}@AfterTextChange({R.id.editText, R.id.helloTextView})voidafterTextChangedOnSomeTextViews(TextView tv, Editable text) {// Something Here}@AfterTextChange(R.id.helloTextView)voidafterTextChangedOnHelloTextView() {// Something Here}或者:123456789101112131415161718192021222324252627282930313233343536@EActivity@OptionsMenu(R.menu.my_menu)publicclassMyActivityextendsActivity {@OptionMenuItemMenuItem menuSearch;@OptionsItem(R.id.menuShare)voidmyMethod() {// You can specify the ID in the annotation, or use the naming convention}@OptionsItemvoidhomeSelected() {// home was selected in the action bar// The "Selected" keyword is optional}@OptionsItembooleanmenuSearch() {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)returntrue;}@OptionsItem({ R.id.menu_search, R.id.menu_delete })voidmultipleMenuItems() {// You can specify multiple menu item IDs in @OptionsItem}@OptionsItemvoidmenu_add(MenuItem item) {// You can add a MenuItem parameter to access it}}@Background 执行:12345@EActivity@OptionsMenu({R.menu.my_menu1, R.menu.my_menu2})publicclassMyActivityextendsActivity {}取消:12345678voidmyMethod() {someBackgroundWork("hello",42);}@BackgroundvoidsomeBackgroundWork(String aParam,longanotherParam) {[...]}非并发执行:1234567891011voidmyMethod() {someCancellableBackground("hello",42);[...]booleanmayInterruptIfRunning =true;BackgroundExecutor.cancelAll("cancellable_task", mayInterruptIfRunning);}@Background(id="cancellable_task")voidsomeCancellableBackground(String aParam,longanotherParam) {[...]}延迟:12345678910voidmyMethod() {for(inti =0; i <10; i++)someSequentialBackgroundMethod(i);}@Background(serial ="test")voidsomeSequentialBackgroundMethod(inti) {SystemClock.sleep(newRandom().nextInt(2000)+1000);Log.d("AA","value : "+ i);}@UiThread UI线程:123@Background(delay=2000)voiddoInBackgroundAfterTwoSeconds() {}延迟:12345678voidmyMethod() {doInUiThread("hello",42);}@UiThreadvoiddoInUiThread(String aParam,longanotherParam) {[...]}优化UI线程:123@UiThread(delay=2000)voiddoInUiThreadAfterTwoSeconds() {}进度值改变:123@UiThread(propagation = Propagation.REUSE)voidrunInSameThreadIfOnUiThread() {}@OnActivityResult123456789101112131415161718@EActivitypublicclassMyActivityextendsActivity {@BackgroundvoiddoSomeStuffInBackground() {publishProgress(0);// Do some stuffpublishProgress(10);// Do some stuffpublishProgress(100);}@UiThreadvoidpublishProgress(intprogress) {// Update progress views}}以上的注释用法基本包含了平常程序中的事件绑定,用AndroidAnnotations框架可以专注于做逻辑开发,最主要是简化代码编写,容易维护。 如有问题可以参考官方文档https://github.com/excilys/androidannotations/wiki/Cookbook, 或者留言。转载务必注明出处。123456789101112131415@OnActivityResult(REQUEST_CODE)voidonResult(intresultCode, Intent data) {}@OnActivityResult(REQUEST_CODE)voidonResult(intresultCode) {}@OnActivityResult(ANOTHER_REQUEST_CODE)voidonResult(Intent data) {}@OnActivityResult(ANOTHER_REQUEST_CODE)voidonResult() {}
- Item clicks with
- Clicks with
评论区