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 |
@EBean public class MyClass { } |
1
2
3
4
5
6
7 |
@EActivity public 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 |
@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
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 |
@EView public 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 |
@EApplication public class MyApplication extends Application { } |
1
2
3
4
5
6
7 |
@EActivity public class MyActivity extends Activity { @App MyApplication application; } |
1
2
3
4 |
@EService public class MyService extends Service { } |
1 |
MyService_.intent(getApplication()).start(); |
1 |
MyService_.intent(getApplication()).stop(); |
1
2
3
4 |
@EReceiver public class MyReceiver extends BroadcastReceiver { } |
1
2
3
4
5
6
7
8
9 |
@EActivity public class MyActivity extends Activity { @Receiver (actions = "org.androidannotations.ACTION_1" ) protected void onAction1() { } } |
1
2
3
4 |
@EProvider public class MyContentProvider extends ContentProvider { } |
1
2
3
4
5
6
7
8
9
10 |
@EActivity public 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 |
@EActivity public class MyActivity extends Activity { @StringRes (R.string.hello) String myHelloString; //不能设置成私有变量 @StringRes String hello; } |
1
2
3
4
5
6
7
8
9
10 |
@EActivity public class MyActivity extends Activity { @ColorRes (R.color.backgroundColor) int someColor; @ColorRes int backgroundColor; } |
1
2
3
4
5
6
7
8
9
10 |
@EActivity public class MyActivity extends Activity { @AnimationRes (R.anim.fadein) XmlResourceParser xmlResAnim; @AnimationRes Animation fadein; } |
1
2
3
4
5
6
7
8
9
10 |
@EActivity public class MyActivity extends Activity { @DimensionRes (R.dimen.fontsize) float fontSizeDimension; @DimensionRes float fontsize; } |
1
2
3
4
5
6
7
8
9
10 |
@EActivity public class MyActivity extends Activity { @DimensionPixelOffsetRes (R.string.fontsize) int fontSizeDimension; @DimensionPixelOffsetRes int fontsize; } |
1
2
3
4
5
6
7
8
9
10 |
@EActivity public class MyActivity extends Activity { @DimensionPixelSizeRes (R.string.fontsize) int fontSizeDimension; @DimensionPixelSizeRes int fontsize; } |
@BooleanRes
@ColorStateListRes
@DrawableRes
@IntArrayRes
@IntegerRes
@LayoutRes
@MovieRes
@TextRes
@TextArrayRes
@StringArrayRes
@Extra12345678910@EActivity
public
class
MyActivity
extends
Activity {
@Extra
(
"myStringExtra"
)
String myMessage;
@Extra
(
"myDateExtra"
)
Date myDateExtraWithDefaultValue =
new
Date();
}
1234567@EActivity
public
class
MyActivity
extends
Activity {
// The name of the extra will be "myMessage",名字必须一致
@Extra
String myMessage;
}
1MyActivity_.intent().myMessage(
"hello"
).start() ;
123456@EActivity
public
class
MyActivity
extends
Activity {
//
@SystemService
NotificationManager notificationManager;
}
123456789101112@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;
}
12345678910111213@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;
}
12345678910public
class
MyActivity
extends
Activity {
//等同于 Activity.onRetainNonConfigurationInstance()
@NonConfigurationInstance
Bitmap someBitmap;
@NonConfigurationInstance
@Bean
MyBackgroundTask myBackgroundTask;
}
12class
=
"brush:java;"
>
@HttpsClient
HttpClient httpsClient;
12345678910111213141516171819202122232425@EActivity
public
class
MyActivity
extends
Activity {
@HttpsClient
(trustStore=R.raw.cacerts,
trustStorePwd=
"changeit"
,
hostnameVerif=
true
)
HttpClient httpsClient;
@AfterInject
@Background
public
void
securedRequest() {
try
{
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();
}
}
12345678910111213@EFragment
public
class
MyFragment
extends
Fragment {
//等同于 Fragment Argument
@FragmentArg
(
"myStringArgument"
)
String myMessage;
@FragmentArg
String anotherStringArgument;
@FragmentArg
(
"myDateExtra"
)
Date myDateArgumentWithDefaultValue =
new
Date();
}
1234MyFragment myFragment = MyFragment_.builder()
.myMessage(
"Hello"
)
.anotherStringArgument(
"World"
)
.build();
123456789101112@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.123456789101112131415161718192021@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) {
}
}
123456789101112131415161718192021@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) {
}
}
1class
=
"brush:java;"
>
//等同于SeekBar.OnSeekBarChangeListener.onProgressChanged(SeekBar, int, boolean)
12345678910111213141516171819@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
12345678910111213141516171819@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
}
12345678910111213141516171819@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
}
12345678910111213141516171819@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
}
123456789101112131415161718192021222324252627282930313233343536@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
}
}
12345@EActivity
@OptionsMenu
({R.menu.my_menu1, R.menu.my_menu2})
public
class
MyActivity
extends
Activity {
}
12345678void
myMethod() {
someBackgroundWork(
"hello"
,
42
);
}
@Background
void
someBackgroundWork(String aParam,
long
anotherParam) {
[...]
}
1234567891011void
myMethod() {
someCancellableBackground(
"hello"
,
42
);
[...]
boolean
mayInterruptIfRunning =
true
;
BackgroundExecutor.cancelAll(
"cancellable_task"
, mayInterruptIfRunning);
}
@Background
(id=
"cancellable_task"
)
void
someCancellableBackground(String aParam,
long
anotherParam) {
[...]
}
12345678910void
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);
}
123@Background
(delay=
2000
)
void
doInBackgroundAfterTwoSeconds() {
}
12345678void
myMethod() {
doInUiThread(
"hello"
,
42
);
}
@UiThread
void
doInUiThread(String aParam,
long
anotherParam) {
[...]
}
123@UiThread
(delay=
2000
)
void
doInUiThreadAfterTwoSeconds() {
}
123@UiThread
(propagation = Propagation.REUSE)
void
runInSameThreadIfOnUiThread() {
}
123456789101112131415161718@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
}
}
123456789101112131415@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() {
}
- Item clicks with
- Clicks with
评论区