找一个Android View对象的当前onClickListener [英] Get current onClickListener of an Android View object

查看:107
本文介绍了找一个Android View对象的当前onClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得当前onClickListener - 并且其他类型的listeners--一个视图对象,以安装新的代理onClickListener它会做一些工作,然后调用转发到原来的听众。

I need to get the current onClickListener --and other kind of listeners-- of a View object in order to install a new proxy-onClickListener which will do some work and then forward the call to the original listener.

非常感谢!

推荐答案

您只需要一点创意!

就在做这样的事情:

子类的View.OnClickListener

Subclass the View.OnClickListener

abstract class ProxyAbleClickListener implements OnClickListener {

    Runnable ProxyAction;

    @Override
    public void onClick(View arg0) {
        if (ProxyAction != null) {
            ProxyAction.run();
        }
    }

    public void setProxyAction(Runnable proxyAction) {
        ProxyAction = proxyAction;
    }
}

/* Somwhere in your code ! */

YourView view = new Button(context);

view.setOnClickListener(new ProxyAbleClickListener() {
    public void onClick(View arg0) {
        // Insert onClick Code here

        super.onClick(arg0);
    }

});

ProxyAbleClickListener listener = (ProxyAbleClickListener) view.getOnClickListener();
listener.setProxyAction(new Runnable() {

    @Override
    public void run() {
        // Insert your proxy code here..

    }
});

请确保有一个子类图片喜欢YourView它覆盖setOnClickListener并有一个参考,听者与getOnClickListner ..

Make sure to have a subclassed view Like YourView that overrides the setOnClickListener and keeps a reference to the listener to access with getOnClickListner..

我没有测试此code以任何方式,把它当作伪$ C $下你需要做的。

I have not tested this code in any way, treat it as pseudo code for what you need to do.

我希望这会了解你一些事情:)

I hope this will Learn you a few things :)

这篇关于找一个Android View对象的当前onClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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