gpt4 book ai didi

android - roboguice 注入(inject)客户

转载 作者:行者123 更新时间:2023-11-30 03:13:29 27 4
gpt4 key购买 nike

我有一个小部件 View 如下:
公共(public)类 RemoteNumView 扩展了 FrameLayout {
我如何调用 Roboguice 就像在 RoboActivity 中一样?如下:

 @InjectView(R.id.btn_remote_control_num_0)
private TextView mText;

完整代码为:

/**
* Created by bbcv on 13-12-12.
*/

public class RemoteNumView extends FrameLayout {
private IService mService;

@InjectView(R.id.btn_remote_control_num_0)
private TextView mText;

public RemoteNumView(Context context) {
super(context);
///
addView(LayoutInflater.from(context).inflate(R.layout.v_remote_control_fun,null));
}

public RemoteNumView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public RemoteNumView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public void setService(IService service){
mService = service;
}
}

有人可以帮忙吗?

最佳答案

通过编写自定义代码解决了这个问题。 Roboguice 写得很糟糕。

 protected void injectViews() {
for (Field field : this.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(InjectView.class)) {
if (Modifier.isStatic(field.getModifiers())) {
throw new UnsupportedOperationException("Views can't be staticaly assigned.");
} else {
if (View.class.isAssignableFrom(field.getType())) {
try {
final InjectView injectView = field.getAnnotation(InjectView.class);
;
final int id = injectView.value();
View view = findViewById(id);
if ((view == null) && Nullable.notNullable(field)) {
throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(), field.getName()));
}
field.setAccessible(true);
field.set(this, view);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
} else {
throw new UnsupportedOperationException("Need view type to assign");
}
}
}
}

关于android - roboguice 注入(inject)客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com