String.valueOf(View) returns 是什么意思?
What does String.valueOf(View) returns?
我正在使用一些日志来调试我的应用程序中的一些问题,当我在 onClick(View view)
.
中使用 String.valueOf(view)
时
我登录了Log.d("Values", "HomeFragment: onClick. view = " + String.valueOf(view));
日志的结果是:
D/Values: HomeFragment: onClick. view = android.widget.RelativeLayout{cca8bb V.E...C.. ...P.... 0,512-1080,1024 #7f0c0079 app:id/button_2}
我想知道那是什么意思。我一直试图从 Android Developers reference 获取信息,但最接近的是 valueOf (Object value)
的定义太笼统了。
我应该去哪里获取String.valueOf()
为特定对象返回的信息?
String.valueOf(object)
只是调用对象的toString
方法。如果你反编译 View
class,你会得到:
// toString method of the View class
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 (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();
}
因此,在您的示例中:
android.widget.RelativeLayout
= class 姓名
cca8bb
= 身份哈希码(十六进制)
V.E...C.. ...P....
= 视图可见、启用遮罩、可点击、按下按钮
0,512-1080,1024
= 左、上-右、下从该视图的父级边缘到该视图边缘的距离(以像素为单位)。
#7f0c0079
= 视图的 id,十六进制
app:id/button_2
= 这个view的字符串id(在你的布局文件中)
我正在使用一些日志来调试我的应用程序中的一些问题,当我在 onClick(View view)
.
String.valueOf(view)
时
我登录了Log.d("Values", "HomeFragment: onClick. view = " + String.valueOf(view));
日志的结果是:
D/Values: HomeFragment: onClick. view = android.widget.RelativeLayout{cca8bb V.E...C.. ...P.... 0,512-1080,1024 #7f0c0079 app:id/button_2}
我想知道那是什么意思。我一直试图从 Android Developers reference 获取信息,但最接近的是 valueOf (Object value)
的定义太笼统了。
我应该去哪里获取String.valueOf()
为特定对象返回的信息?
String.valueOf(object)
只是调用对象的toString
方法。如果你反编译 View
class,你会得到:
// toString method of the View class
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 (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();
}
因此,在您的示例中:
android.widget.RelativeLayout
= class 姓名cca8bb
= 身份哈希码(十六进制)V.E...C.. ...P....
= 视图可见、启用遮罩、可点击、按下按钮0,512-1080,1024
= 左、上-右、下从该视图的父级边缘到该视图边缘的距离(以像素为单位)。#7f0c0079
= 视图的 id,十六进制app:id/button_2
= 这个view的字符串id(在你的布局文件中)