給一個View設置監聽點擊事件是再普通不過的事情,好比java
view.setOnClickListener(onClickListener);
另一種作法是直接在XML佈局裏面指定View點擊時候的回調方法,首先須要在Activity中編寫用於回調的方法,好比android
public void onClickView(View view){ // do something }
而後在XML設置View的android:onClick
屬性app
<View android:layout_width="match_parent" android:layout_height="match_parent" android:onClick="onClickView" />
有的時候從XML佈局裏直接設定點擊事件會比較方便(特別是在寫DEMO項目的時候),這種作法平時用的人並很少,從使用方式上大體能猜出來,View應該是在運行的時候,使用反射的方式從Activity找到「onClickView」方法並調用,由於這種作法並無用到任何接口。ide
接下來,咱們能夠從源碼中分析出View是怎麼觸發回調方法的。佈局
View有5個構造方法,第一個是內部使用的,平時在Java代碼中直接建立View實例用的是第二種方法,而從XML佈局渲染出來的View實例最後都是要調用第五種方法。單元測試
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { this(context); final TypedArray a = context.obtainStyledAttributes( attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes); for (int i = 0; i < N; i++) { int attr = a.getIndex(i); switch (attr) { …… // 處理onClick屬性 case R.styleable.View_onClick: if (context.isRestricted()) { throw new IllegalStateException("The android:onClick attribute cannot " + "be used within a restricted context"); } final String handlerName = a.getString(attr); if (handlerName != null) { // 給當前View實例設置一個DeclaredOnClickListener監聽器 setOnClickListener(new DeclaredOnClickListener(this, handlerName)); } break; } } }
處理onClick屬性的時候,先判斷View的Context是否isRestricted,若是是就拋出一個IllegalStateException異常。看看isRestricted方法測試
/** * Indicates whether this Context is restricted. * * @return {@code true} if this Context is restricted, {@code false} otherwise. * * @see #CONTEXT_RESTRICTED */ public boolean isRestricted() { return false; }
isRestricted是用於判斷當前的Context實例是否出於被限制的狀態,按照官方的解釋,處限制狀態的Context,會忽略某些特色的功能,好比XML的某些屬性,很明顯,咱們在研究的android:onClick
屬性也會被忽略。this
a restricted context may disable specific features. For instance, a View associated with a restricted context would ignore particular XML attributes.spa
不過isRestricted方法是Context中爲數很少的有具體實現的方法(其他基本是抽象方法),這裏直接返回false,並且這個方法只有在ContextWrapper和MockContext中有重寫代理
public class ContextWrapper extends Context { Context mBase; @Override public boolean isRestricted() { return mBase.isRestricted(); } } public class MockContext extends Context { @Override public boolean isRestricted() { throw new UnsupportedOperationException(); } }
ContextWrapper中也只是代理調用mBase的isRestricted,而MockContext是寫單元測試的時候纔會用到,因此這裏的isRestricted基本只會返回false,除非使用了自定義的ContextWrapper並重寫了isRestricted。
回到View,接着的final String handlerName = a.getString(attr);
其實就是拿到了android:onClick="onClickView"
中的「onClickView」這個字符串,接着使用了當前View的實例和「onClickView」建立了一個DeclaredOnClickListener實例,並設置爲當前View的點擊監聽器。
/** * An implementation of OnClickListener that attempts to lazily load a * named click handling method from a parent or ancestor context. */ private static class DeclaredOnClickListener implements OnClickListener { private final View mHostView; private final String mMethodName; private Method mMethod; public DeclaredOnClickListener(@NonNull View hostView, @NonNull String methodName) { mHostView = hostView; mMethodName = methodName; } @Override public void onClick(@NonNull View v) { if (mMethod == null) { mMethod = resolveMethod(mHostView.getContext(), mMethodName); } try { mMethod.invoke(mHostView.getContext(), v); } catch (IllegalAccessException e) { throw new IllegalStateException( "Could not execute non-public method for android:onClick", e); } catch (InvocationTargetException e) { throw new IllegalStateException( "Could not execute method for android:onClick", e); } } @NonNull private Method resolveMethod(@Nullable Context context, @NonNull String name) { while (context != null) { try { if (!context.isRestricted()) { return context.getClass().getMethod(mMethodName, View.class); } } catch (NoSuchMethodException e) { // Failed to find method, keep searching up the hierarchy. } if (context instanceof ContextWrapper) { context = ((ContextWrapper) context).getBaseContext(); } else { // Can't search up the hierarchy, null out and fail. context = null; } } final int id = mHostView.getId(); final String idText = id == NO_ID ? "" : " with id '" + mHostView.getContext().getResources().getResourceEntryName(id) + "'"; throw new IllegalStateException("Could not find method " + mMethodName + "(View) in a parent or ancestor Context for android:onClick " + "attribute defined on view " + mHostView.getClass() + idText); } }
到這裏就清楚了,當點擊View的時候,DeclaredOnClickListener實例的「onClick」方法會被調用,接着會調用「resolveMethod」方法,使用反射的方式從View的Context中找一個叫「onClickView」方法,這個方法有一個View類型的參數,最後再使用反射調用該方法。要注意的是,「onClickView」方法必須是public類型的,否則反射調用時會拋出IllegalAccessException異常。
同時從源碼也能看出,使用android:onClick
設置點擊事件的方式是從Context裏面查找回調方法的,因此若是對於在Fragment的XML裏建立的View,是沒法經過這種方式綁定Fragment中的回調方法的,由於Fragment自身並非一個Context,這裏的View的Context實際上是FragmentActivity,這也意味着使用這種方式可以快速地從Fragment中回調到FragmentActivity。
此外,從DeclaredOnClickListener類的註釋也能看出android:onClick
的功能,主要是起到懶加載的做用,只有到點擊View的時候,纔會知道哪一個方法是用於點擊回調的。
最後,特別須要補充說明的是,使用android:onClick
給View設置點擊事件,就意味着要在Activity裏添加一個非接口的public方法。如今Android的開發趨勢是「不要把業務邏輯寫在Activity類裏面」,這樣作有利於項目的維護,防止Activity爆炸,因此儘可能不要在Activity裏出現非接口、非生命週期的public方法。所以,貿然使用android:onClick
可能會「污染」Activity。