AndroidAnnotations——Injecting Views视图注入
2014-04-06 15:40
204 查看
AndroidAnnotation
目录(?)[+]
Injecting Views视图注入
ViewById
AfterViews
The
findViewById() method. The view id can be set in the annotation parameter, ie
must not be private.
注解标识一个activity字段应该和布局中的一致的视图组件绑定在一起。就和调用
findViewById()方法一样。view id可以在注解参数中添加,比如
id,将默认使用字段名。这个字段不能是private类型的,一般使用默认类型或者protected类型。
Usage example:用法:
The
注解标识在视图绑定操作发生后,有个方法需要被调用,一般可以使用init方法名。
When
on methods to write code that depends on views.当
方法调用后,
字段还没有设置好。因此,你可以用
注解绑定的方法添加处理视图相关的代码。
Usage example:用法:
You can annotate multiple methods with
你可以用
Recall that injection is always made as soon as possible. Therefore, it's safe to use any field annotated, e.g., with @Extra or @InstanceState in @AfterViews methods as these tags don't require any view to be set (as @AfterViews do). Therefore
you can safely assume that such fields will be already initialized with their intended values in methods annotated with @AfterViews:
回想一下,注入总是以最快的速度完成。因此,在@AfterViews方法中使用任何不需要设置视图(像@AfterViews要做的)注解字段,比如@Extra
或者 @InstanceState,都是安全的。所以,你可以放心地假设这些字段在加了@AfterViews注解的方法中初始化值是安全的。
目录(?)[+]
Injecting Views视图注入
ViewById
AfterViews
Injecting Views视图注入
Since AndroidAnnotations 1.0
@ViewById
The @ViewByIdannotation indicates that an activity field should be bound with the corresponding View component from the layout. It is the same as calling the
findViewById() method. The view id can be set in the annotation parameter, ie
@ViewById(R.id.myTextView). If the view id is not set, the name of the field will be used. The field
must not be private.
@ViewById
注解标识一个activity字段应该和布局中的一致的视图组件绑定在一起。就和调用
findViewById()方法一样。view id可以在注解参数中添加,比如
@ViewById(R.id.myTextView)。假如没有设置view
id,将默认使用字段名。这个字段不能是private类型的,一般使用默认类型或者protected类型。
Usage example:用法:
@EActivity
public class MyActivity extends Activity {
// Injects R.id.myEditText@ViewById
EditText myEditText;@ViewById(R.id.myTextView)
TextView textView;
}
@AfterViews
The @AfterViewsannotation indicates that a method should be called after the views binding has happened.
@AfterViews
注解标识在视图绑定操作发生后,有个方法需要被调用,一般可以使用init方法名。
When
onCreate()is called,
@ViewByIdfields are not set yet. Therefore, you can use
@AfterViews
on methods to write code that depends on views.当
onCreate()
方法调用后,
@ViewById
字段还没有设置好。因此,你可以用
@AfterViews
注解绑定的方法添加处理视图相关的代码。
Usage example:用法:
@EActivity(R.layout.main)
public class MyActivity extends Activity {@ViewById
TextView myTextView;@AfterViews
void updateTextWithDate() {
myTextView.setText("Date: " + new Date());
}
[...]
You can annotate multiple methods with
@AfterViews. Don't forget that you should not use any view field in
onCreate():
你可以用
@AfterViews注解多个方法。不要忘记在
onCreate()方法中不能使用任何视图字段。
@EActivity(R.layout.main)
public class MyActivity extends Activity {@ViewById
TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// DON'T DO THIS !! It will throw a NullPointerException, myTextView is not set yet.
// myTextView.setText("Date: " + new Date());
}
[...]
Recall that injection is always made as soon as possible. Therefore, it's safe to use any field annotated, e.g., with @Extra or @InstanceState in @AfterViews methods as these tags don't require any view to be set (as @AfterViews do). Therefore
you can safely assume that such fields will be already initialized with their intended values in methods annotated with @AfterViews:
回想一下,注入总是以最快的速度完成。因此,在@AfterViews方法中使用任何不需要设置视图(像@AfterViews要做的)注解字段,比如@Extra
或者 @InstanceState,都是安全的。所以,你可以放心地假设这些字段在加了@AfterViews注解的方法中初始化值是安全的。
@EActivity(R.layout.main)
public class MyActivity extends Activity {@ViewById
TextView myTextView;
@InstanceState
Integer textPosition;@AfterViews
void updateTextPosition() {
myTextView.setSelection(textPosition); //Sets the cursor position of myTextView
}
[...] // The remaining code must keep textPosition updated
相关文章推荐
- AndroidAnnotations——Injecting Views视图注入
- Android Databinding 从入门到转行(三)在xml视图将ViewModel成员注入到View的setXXX方法
- AndroidAnnotations——Injecting project Resources注入项目资源
- AndroidAnnotations——Listening to AdapterViewEvents监听适配器视图事件
- Android:xUtils3浅析(一)——视图注入
- Android视图注入框架butterknife
- android 视图自动注入的第三方库-ButterKnife插件用法介绍
- AndroidAnnotations——Injecting Extras注入Extras
- android studio 配置androidannotations注入框架
- AndroidAnnotations——Injecting SystemServices注入系统服务
- Dagger,ButterKnife,Roboguide,AndroidAnnotations框架的依赖注入浅析
- AndroidAnnotations——Injecting html注入html文本
- AndroidAnnotations——Injecting FragmentArg注入Fragment参数
- AndroidAnnotations——Injecting project Resources注入项目资源
- AndroidAnnotations——Reinject NonConfiguration Instance重新注入NonConfiguration 实例
- AndroidAnnotations——Injecting SystemServices注入系统服务
- AndroidAnnotations——Listening to AdapterViewEvents监听适配器视图事件
- AndroidAnnotations——Injecting Extras注入Extras
- 【FastDev4Android框架开发】RecyclerView完全解析之结合AA(Android Annotations)注入框架实例(三十)
- AndroidAnnotations——Injecting html注入html文本