博不如精

完美靠的是质量而非数量。世上的好东西时常小而难求,过多必无誉。只靠广博则难免成为平庸之辈,有所谓通才总想在学业上门门皆精,结果却常常是门门皆瘟。学业专攻必结硕果;若从事重要的事务,则必得美名。...

AndroidAnnotations——Injecting Views视图注入

Injecting Views视图注入

Since AndroidAnnotations 1.0

@ViewById


The @ViewById annotation 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 @AfterViews annotation indicates that a method should be called after the views binding has happened.
 @AfterViews 注解标识在视图绑定操作发生后,有个方法需要被调用,一般可以使用init方法名。

When onCreate() is called, @ViewById fields 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.
阅读更多
文章标签: AndroidAnnotation
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭