找一个Android View对象的当前onClickListener找一个、对象、View、Android

由网友(京城刽子手号.)分享简介:我需要获得当前onClickListener - 并且其他类型的listeners--一个视图对象,以安装新的代理onClickListener它会做一些工作,然后调用转发到原来的听众。I need to get the current onClickListener --and other kind of lis...

我需要获得当前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 :)

阅读全文

相关推荐

最新文章