Get current onClickListener of an Android View object

為{幸葍}努か 提交于 2019-12-30 18:49:45

问题


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.

Many thanks!!!


回答1:


You just need to be a little creative!

Just do something like this:

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..

    }
});

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

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 :)



来源:https://stackoverflow.com/questions/6610143/get-current-onclicklistener-of-an-android-view-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!