Get current onClickListener of an Android View object
I need to ge开发者_如何学Pythont 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!!!
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 :)
精彩评论