/**
* Android源碼
* Indicates whether this View is currently in edit mode. A View is usually
* in edit mode when displayed within a developer tool. For instance, if
* this View is being drawn by a visual user interface builder, this method
* should return true.
*
* Subclasses should check the return value of this method to provide
* different behaviors if their normal behavior might interfere with the
* host environment. For instance: the class spawns a thread in its
* constructor, the drawing code relies on device-specific features, etc.
*
* This method is usually checked in the drawing code of custom widgets.
*
* @return True if this View is in edit mode, false otherwise.
*/
public boolean isInEditMode() {
return false;
}
學習大佬寫的代碼的時候發現了這個方法。這個方法是andorid.view.View中的代碼,它的返回值也是很有意思。看註釋是在編輯模式下返回true,否則返回false。
那麼問題來了。。。什麼叫“edit mode”編輯模式???上面一大段註釋看的我一臉懵逼,反正我是沒整明白啥叫編輯模式。。。
直到後來經過大佬指導,才知道所謂的“edit mode”就是指AndroidStudio環境下的xml佈局文件可視化編輯。。。尤其是在自定義View的時候,當我們把自定義的View通過xml佈局文件展示出來的時候,AndroidStudio會調用這個View的構造方法來生成圖像,使得我們在寫xml文件的時候可以實時看到View的變化。所以只要我們通過xml編輯自定義View的時候,就被稱爲“edit mode”編輯模式。
另外isInEditMode方法的返回值就會告訴我們是否處於這樣一個模式下,很多時候處於“edit mode”的情況下,有些代碼是不需要執行的。因爲xml編輯器的可視化只是方便我們編寫佈局而已,並不是一個Andorid 虛擬機,我們可以通過這個方法來避免執行多餘的代碼。用以提高AndoridStudio的效率
if(isInEditMode()) return;