How to get listener of a view

前端 未结 4 1506
清酒与你
清酒与你 2020-12-28 21:14

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 lis

相关标签:
4条回答
  • 2020-12-28 21:36

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

    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.

    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);
    }
    

    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.

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

    Found the final tutorial here: http://andwise.net/?p=161

    0 讨论(0)
  • 2020-12-28 21:40

    Make two instances of OnCLickListener and assign first or second to button:

        Button b = (Button) findViewById(R.id.Button1);
    
        OnClickListener listener_new =  new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("APP", "NEW CLICK LISTENER ACTIVE");
            }
        };
    
        OnClickListener listener_old =  new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("APP", "OLD CLICK LISTENER ACTIVE");
            }
        };
    
        //setting listener
        b.setOnClickListener(listener_old);
        b.callOnClick();
    
                //changing listener
        b.setOnClickListener(listener_new);
        b.callOnClick();
    
                //return your old listener!
            b.setOnClickListener(listener_old);
            b.callOnClick();
    

    ADDED:

    OnClickListener is protected field of Button class, inherited from View class. Name of the field "mOnClickListener". I can't get it even through reflection.

    void getListener(Button b) {
    java.lang.reflect.Field field =  getClass().getSuperclass().getSuperclass().getDeclaredField("mOnClickListener");
    }
    

    So You can't get existing listener of the Button if You don't have access to code where it created.

    But if You have access to objects of Activity (and we know You have because setting new listener to button), You could add your button with your listener on that activity. Make existing button invisible. And than rollback when necessary.

    0 讨论(0)
  • 2020-12-28 21:42
    public abstract class ReflectionUtils {
    
        private static final String listenerInfoFieldName = "mListenerInfo";
        private static final String onCLickListenerFieldName = "mOnClickListener";
    
        public static OnClickListener getOnClickListener(View view){
            Object listenerInfo = ReflectionUtils.getValueFromObject(view, listenerInfoFieldName, Object.class);
            return ReflectionUtils.getValueFromObject(listenerInfo, onCLickListenerFieldName, View.OnClickListener.class);
        }
    
        public static <T> T getValueFromObject(Object object, String fieldName, Class<T> returnClazz){
            return getValueFromObject(object, object.getClass(), fieldName, returnClazz);
        }
    
        private static <T> T getValueFromObject(Object object, Class<?> declaredFieldClass, String fieldName, Class<T> returnClazz){
            try {
                Field field = declaredFieldClass.getDeclaredField(fieldName);
                field.setAccessible(true);
                Object value = field.get(object);
                return returnClazz.cast(value);
            } catch (NoSuchFieldException e) {
                Class<?> superClass = declaredFieldClass.getSuperclass();
                if(superClass != null){
                    return getValueFromObject(object, superClass, fieldName, returnClazz);
                }
            } catch (IllegalAccessException e) {
            }
            return null;
        }
    
    }
    

    Calling OnClickListener onClickListener = ReflectionUtils.getOnClickListener(myView); on myView will give you the myView's listener that you are looking for.

    0 讨论(0)
  • 2020-12-28 21:49

    create classes that implements OnClickListener

    public static class MyClickListener1 implements OnClickListener{
        Activity mActivity;
        MyClickListener1(Acivity activity){
          mActivity=activity;
        }
        @Override
        public void onClick(View v) {
                //do something
        }
    }
    
    public static class MyClickListener2 implements OnClickListener{
        @Override
        public void onClick(View v) {
                //do something
        }
    }
    

    and in your code you can easily use them:

    btn.setOnClickListener(new MyClickListener1(this));
    btn.setOnClickListener(new MyClickListener2());
    

    or you can create instances and reuse them:

    OnClickListener listener1 = new MyClickListener1(this);
    OnClickListener listener2 = new MyClickListener2();
    btn.setOnClickListener(listener1);
    btn.setOnClickListener(listener2);
    

    you can also define a constructor to pass whatever you need in these classes. I usually pass the activity like in MyClickListener1

    EDIT: If you want to have the listener like object in the button, you can use the tag.

    btn.setTag(listener1);
    btn.setOnClickListener(listener1);
    

    and then to get it use

    OnClickListener old_listener = (OnClickListenr)btn.getTag();
    
    0 讨论(0)
提交回复
热议问题