我将 View 记录到 logcat 以区分不同的 View 以进行调试。
我注意到输出(主要由 View.toString 引起)是这样的:
com.example.app.CustomView{7b14550 IFE...C.. ........ 0,0-1440,315}
大括号之间的每个部分是什么意思?
更新:只是认为答案可能在查看源代码中并看了一下。对于任何想知道这一点的人,有一个 toString() 方法可以解释每个值。
最佳答案
当您对任何给定类调用 .toString() 时,除非它被覆盖,否则它将调用 toString()
,因为它在 Object 中定义。每个类都以某种方式将 Object 作为根。如果扩展一个类,则该类的父类(super class)是 Object。如果扩展类扩展了一个不同的类,则该类的父类(super class)是 Object。你明白了。所以从对象文档:
/** * Returns a string representation of the object. In general, the * {@code toString} method returns a string that * "textually represents" this object. The result should * be a concise but informative representation that is easy for a * person to read. * It is recommended that all subclasses override this method. * <p> * The {@code toString} method for class {@code Object} * returns a string consisting of the name of the class of which the * object is an instance, the at-sign character `{@code @}', and * the unsigned hexadecimal representation of the hash code of the * object. In other words, this method returns a string equal to the * value of: * <blockquote> * <pre> * getClass().getName() + '@' + Integer.toHexString(hashCode()) * </pre></blockquote> * * @return a string representation of the object. */ public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
意思是除非你覆盖它,否则它会打印出来。这意味着它会打印出类哈希码的十六进制字符串。
因此,除非它被覆盖以打印出其他内容,否则它会打印出类哈希码的十六进制字符串。
但是对于 View 类,这是它具有的方法:
public String toString() { StringBuilder out = new StringBuilder(128); out.append(getClass().getName()); out.append('{'); out.append(Integer.toHexString(System.identityHashCode(this))); out.append(' '); switch (mViewFlags&VISIBILITY_MASK) { case VISIBLE: out.append('V'); break; case INVISIBLE: out.append('I'); break; case GONE: out.append('G'); break; default: out.append('.'); break; } out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.'); out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.'); out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D'); out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.'); out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.'); out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.'); out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.'); out.append((mViewFlags&CONTEXT_CLICKABLE) != 0 ? 'X' : '.'); out.append(' '); out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.'); out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.'); out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.'); if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) { out.append('p'); } else { out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.'); } out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.'); out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.'); out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.'); out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.'); out.append(' '); out.append(mLeft); out.append(','); out.append(mTop); out.append('-'); out.append(mRight); out.append(','); out.append(mBottom); final int id = getId(); if (id != NO_ID) { out.append(" #"); out.append(Integer.toHexString(id)); final Resources r = mResources; if (id > 0 && Resources.resourceHasPackage(id) && r != null) { try { String pkgname; switch (id&0xff000000) { case 0x7f000000: pkgname="app"; break; case 0x01000000: pkgname="android"; break; default: pkgname = r.getResourcePackageName(id); break; } String typename = r.getResourceTypeName(id); String entryname = r.getResourceEntryName(id); out.append(" "); out.append(pkgname); out.append(":"); out.append(typename); out.append("/"); out.append(entryname); } catch (Resources.NotFoundException e) { } } } out.append("}"); return out.toString(); }
打印出来的意思是:
7b14550 - hash code identifying the view. Note that this will NOT remain constant, as it after all is a hash.
I - the view is invisible
F - the view is focusable
E - the view is enabled
C - the view is clickable
0,0-1440,315 - indicates left, top, right and bottom offset.
但是正如您从上面的代码中看到的,有许多不同的键需要注意。
括号可以指导您了解打印出的哪些详细信息与您调用 .toString 的 View 相关联。据我所知,除了向您显示打印出的文本属于该类之外,它们没有任何功能。
您可以覆盖 toString 并让它打印出您想要的任何数据:字段、方法的结果,或者什么都不做
future 的小贴士
下次您想知道方法的作用时(假设您使用 IDE),CTRL+左键单击有问题的方法/字段。如果您使用其他程序,只要您正在运行受支持的 LSP 支持的语义自动完成,就应该仍然有打开目标函数的方法。查看适用于您的编辑器的条件。
无论哪种方式,这都会打开目标类并向您显示方法源代码和开发人员留下的任何潜在文档。
关于java - View.toString() 读出含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44159455/