您可以使用... REFLECTION来做到这一点。*击鼓*
这条路线充满了危险。
我不推荐它,因为类的内部结构可以随时更改,但如果确实无法避免,您可以按照以下方式在 Android 4.2.2 之前执行此操作:
/**
* Returns the current View.OnClickListener for the given View
* @param view the View whose click listener to retrieve
* @return the View.OnClickListener attached to the view; null if it could not be retrieved
*/
public View.OnClickListener getOnClickListener(View view) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return getOnClickListenerV14(view);
} else {
return getOnClickListenerV(view);
}
}
//Used for APIs lower than ICS (API 14)
private View.OnClickListener getOnClickListenerV(View view) {
View.OnClickListener retrievedListener = null;
String viewStr = "android.view.View";
Field field;
try {
field = Class.forName(viewStr).getDeclaredField("mOnClickListener");
retrievedListener = (View.OnClickListener) field.get(view);
} catch (NoSuchFieldException ex) {
Log.e("Reflection", "No Such Field.");
} catch (IllegalAccessException ex) {
Log.e("Reflection", "Illegal Access.");
} catch (ClassNotFoundException ex) {
Log.e("Reflection", "Class Not Found.");
}
return retrievedListener;
}
//Used for new ListenerInfo class structure used beginning with API 14 (ICS)
private View.OnClickListener getOnClickListenerV14(View view) {
View.OnClickListener retrievedListener = null;
String viewStr = "android.view.View";
String lInfoStr = "android.view.View$ListenerInfo";
try {
Field listenerField = Class.forName(viewStr).getDeclaredField("mListenerInfo");
Object listenerInfo = null;
if (listenerField != null) {
listenerField.setAccessible(true);
listenerInfo = listenerField.get(view);
}
Field clickListenerField = Class.forName(lInfoStr).getDeclaredField("mOnClickListener");
if (clickListenerField != null && listenerInfo != null) {
retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo);
}
} catch (NoSuchFieldException ex) {
Log.e("Reflection", "No Such Field.");
} catch (IllegalAccessException ex) {
Log.e("Reflection", "Illegal Access.");
} catch (ClassNotFoundException ex) {
Log.e("Reflection", "Class Not Found.");
}
return retrievedListener;
}