如何获得视图监听 [英] How to get listener of a view

查看:312
本文介绍了如何获得视图监听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的服务,与其他应用程序进行交互。它注册听众的意见(按钮,textviews,...),已经有听众。我需要将它们与我自己的听众(作品)代替,做一些东西,然后注销我的听众,恢复旧的。

I write service that interacts with other apps. It registers listeners on views (buttons, textviews,...), that already have listeners. I need to replace them with my own listeners (works), do some stuff and then unregister my listeners and restore the old ones.

  1. 在一个应用程序与一个onClickListener一个按钮运行
  2. 在我的服务注册的onClickListener的UI线程里面+做点什么
  3. 在我的服务恢复旧的监听

这将是容易的,如果有一个 view.getOnClickListener - 方法。然后,我可以保存旧的,更换新的听众,当我完成了。

It would be easy, if there was a view.getOnClickListener -method. Then I could save the old ones and replace the new listeners when I'm done.

有没有办法从一个视图得到听众或有更多的相同类型的一个监听器绑定到一个看法?

Is there any way to get listeners from a view or have more that one listener of the same type bound to one view?

Button btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            //do something
        }
    });
// I need to do, but found no solution for that.
View.OnClickListener oldListener = btn.getOnClickListener();

如果我注册新的听众一个视图,旧的将被覆盖,对不对?这将是也没关系,如果两个监听器(新与旧)存在在同一时间。只有旧的不能走了。

If I register the new listeners to a view, the old ones are overridden, right? It would be also okay if both listeners ("new" and "old") exist at the same time. Only the old ones must not be gone.

编辑:不幸的是我没有可能性,以节省听众的分配。我需要从视图组件让它倒退。

edit: Unfortunately I have no possibility to save the listener on assignment. I need to get it backwards from the view component.

感谢

推荐答案

由于米哈伊尔的提示(感谢您的:)))与的隐藏API 的,我已经找到了解决方案,以获得监听分配后回:

Thanks to mihail's hint (thanks for that :)) )with the hidden API, I've found a solution to get a listener back after assignment:

android.view.View 类有一个嵌套类静态类ListenerInfo 是在查看存储所有听众(API 14+)。在旧版本的听众都在私人领域 android.view.View

The android.view.View class has a nested class static class ListenerInfo that stores all listeners on a View (API 14+). In older versions the listeners are private fields in the android.view.View.

本场可与反思访问。在我的情况(API 14+),

The field can be accessed with reflection. In my case (API 14+),

// get the nested class `android.view.View$ListenerInfo`
Field listenerInfoField = null;
listenerInfoField = Class.forName("android.view.View").getDeclaredField("mListenerInfo");
if (listenerInfoField != null) {
    listenerInfoField.setAccessible(true);
}
Object myLiObject = null;
myLiObject = listenerInfoField.get(myViewObj);

// get the field mOnClickListener, that holds the listener and cast it to a listener
Field listenerField = null;
listenerField = Class.forName("android.view.View$ListenerInfo").getDeclaredField("mOnClickListener")
if (listenerField != null && myLiObject != null) {
    View.OnClickListener myListener = (View.OnClickListener) listenerField.get(myLiObject);
}

之后code(我错过了很多的try-catch块的),在myListener的对象持有onClickListener,已经被之前匿名宣布视图的实例。它也适用于任何其他的侦听器,只需更换mOnClickListener参数你需要在反思一个正确丢。

After that code (I missed a lot of try-catch-blocks), the myListener object holds the instance of the onClickListener, that has been anonymously declared to the view before. It also works with any other listener, just replace the "mOnClickListener parameter" with the one you need in the reflection and cast it correctly.

请注意在即将到来的版本code的变化可以使不工作了。

Note that code changes in upcoming versions can make that not working anymore.

找到最终的教程: http://andwise.net/?p=161

这篇关于如何获得视图监听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆