xUtils支持Preference的两个事件注解,分别是:
ViewUtils中的注解部分:
我用的是xUtils 2.6.14版本,通过解读源码发现,这个版本虽然有定义Preference事件注解,
但是,却并没有对注解的处理,所以,需要自己补充,具体方法如下:
在源文件的EventListenerManager.java 函数addEventMethod中做如下修改:
之后就可以正常如下使用了:
OnPreferenceChange:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@EventBase(
listenerType = Preference.OnPreferenceChangeListener.class,
listenerSetter = "setOnPreferenceChangeListener",
methodName = "onPreferenceChange")
public @interface OnPreferenceChange {
String[] value();
}
OnPreferenceClick:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@EventBase(
listenerType = Preference.OnPreferenceClickListener.class,
listenerSetter = "setOnPreferenceClickListener",
methodName = "onPreferenceClick")
public @interface OnPreferenceClick {
String[] value();
}
ViewUtils中的注解部分:
我用的是xUtils 2.6.14版本,通过解读源码发现,这个版本虽然有定义Preference事件注解,
但是,却并没有对注解的处理,所以,需要自己补充,具体方法如下:
在源文件的EventListenerManager.java 函数addEventMethod中做如下修改:
public static void addEventMethod(
ViewFinder finder,
ViewInjectInfo info,
Annotation eventAnnotation,
Object handler,
Method method) {
try {
Preference pref = null;
View view =null;
if(info.value instanceof String)
pref = finder.findPreference((String)info.value);
if(info.value instanceof Integer)
view = finder.findViewByInfo(info);
if (view != null || pref != null) {
EventBase eventBase = eventAnnotation.annotationType().getAnnotation(EventBase.class);
Class<?> listenerType = eventBase.listenerType();
String listenerSetter = eventBase.listenerSetter();
String methodName = eventBase.methodName();
boolean addNewMethod = false;
Object listener = listenerCache.get(info, listenerType);
DynamicHandler dynamicHandler = null;
if (listener != null) {
dynamicHandler = (DynamicHandler) Proxy.getInvocationHandler(listener);
addNewMethod = handler.equals(dynamicHandler.getHandler());
if (addNewMethod) {
dynamicHandler.addMethod(methodName, method);
}
}
if (!addNewMethod) {
dynamicHandler = new DynamicHandler(handler);
dynamicHandler.addMethod(methodName, method);
listener = Proxy.newProxyInstance(
listenerType.getClassLoader(),
new Class<?>[]{listenerType},
dynamicHandler);
listenerCache.put(info, listenerType, listener);
}
if(view !=null)
{
Method setEventListenerMethod = view.getClass().getMethod(listenerSetter, listenerType);
setEventListenerMethod.invoke(view, listener);
}
if(pref !=null)
{
Method setEventListenerMethod = pref.getClass().getMethod(listenerSetter, listenerType);
setEventListenerMethod.invoke(pref, listener);
}
}
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
}
}
之后就可以正常如下使用了:
public class PrefActivity extends PreferenceActivity {
@PreferenceInject("test")
public Preference test;
@OnPreferenceClick("test")
public boolean onPreferenceClick(Preference preference)
{
Toast.makeText(getApplicationContext(), "clicked~", Toast.LENGTH_SHORT).show();
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_test);
ViewUtils.inject(this);
}
}