有没有办法与提供者绑定,该提供者在 Google Guice 中解释目标的注释值?
例子:
bind(Resource.class)
.annotatedWith(MyAnnotation.class)
.toProvider(new MyProvider<MyAnnotation, Resource>{
public Resource get(MyAnnotation anno){
return resolveResourceByAnnoValue(anno.value());
}
});
我想通过带注释的绑定来初始化 Android Activity 类的字段。它应该必须通过它的唯一 ID 来获取多个资源。
原始方式:
public class TestActivity extends Activity{
private TextView textView;
private Button testButton;
public void onAfterCreate(...){
// set UI declaration resource.
setContentView(R.layout.activity_test);
// initialize fields, it must be done after setting ui definition.
textView = (TextView) findViewById(R.id.textView);
.... initialize other fields, hook them...
...
}
我想以声明的方式绑定 UI 和它的字段,而不是务实地喜欢上面:
@ResourceID(R.layout.activity_test)
public class TestActivity extends InjectiveActivity{
@ResourceID(R.id.textView) // Auto generated static resource id constant
private TextView textView;
@ResourceID(R.id.testButton)
private Button testButton;
...
}