/**
* 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;