View.toString()读出意义 [英] View.toString() readout meaning

查看:92
本文介绍了View.toString()读出意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将视图记录到logcat以区分用于调试目的的不同视图。

I'm logging views out to the logcat to distinguish between different views for debug purposes.

我注意到的是输出(主要由View引起) .toString)是这样的:

What I've noticed is that the output (caused essentially by View.toString) is something like this:

com.example.app.CustomView{7b14550 IFE...C.. ........ 0,0-1440,315}

卷曲之间的每个部分是什么括号是什么意思?

What does each section between the curly brackets mean?

更新:只是认为答案可能在View源代码中并看了一下。对于任何想要了解这一点的人来说,有一个toString()方法可以解释每个值。

UPDATE: Just thought the answer may be in the View source code and had a look. For anyone looking to know this there is a toString() method that explains each of the values.

推荐答案

当你调用.toString时()到任何给定的类,除非被覆盖,它将调用Object中的toString。每个类都以某种或其他方式将Object作为根。如果扩展一个类,那么该类的超类就是Object。如果类扩展扩展了另一个类,那么该类的超类是Object。你明白这个想法。所以从Object文档:

When you call .toString() to any given class, unless it is overridden, it will call toString in Object. Every class has Object as a root in some or another way. If you extend one class, that class' superclass is Object. If the class extended extends a different class, that class' superclass is Object. You get the idea here. So from the Object documentation:

/**
 * 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());
}

除非你覆盖它,否则它会打印出来。这意味着它打印出类'哈希码的十六进制字符串。

Meaning unless you override it, it prints out that. Meaning it prints out the hex string of the class' hash code.

因此,除非被覆盖以打印出其他内容,否则会打印出类的十六进制字符串' hashcode。

So unless it is overridden to print out something else, it prints out the hex string of the class' hashcode.

但是对于View类,这是它的方法:

But in case of the class View, this is the method it has:

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
`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 in the screen.

但是从上面的代码中可以看出,有许多不同的键需要注意。

But as you see from the code above, there are many different keys to be aware of.

括号用于指导您打印出的详细信息是否已连接到您调用.toString的视图。据我所知,他们没有任何功能在外面向你显示打印出来的文本属于那个类。

And the brackets are there to guide you as to what of the details that are printed out is connected to the View you have called .toString on. As far as I have understood, they have no function outside showing you what of the text printed out belongs to that class.

你可以覆盖toString并打印出你想要的任何数据:字段,方法结果,或者什么都不做

You can override toString and make it print out whatever data you want: fields, results of methods, or just do nothing

下次你想知道什么一个方法,CTRL +左键单击有问题的方法/字段。这将打开目标类,并向您显示开发人员留下的方法和任何可能的文档

Next time you wonder what a method does, CTRL+Left click on the method/field in question. This opens the target class and shows you the method and any potential documentation left by the developer(s)

这篇关于View.toString()读出意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆