在Android SDK覆盖方法toString中,使用actionToString方法作为公共静态修改器。如果您打开源代码,您必须看到:
@Override
public String toString() {
StringBuilder msg = new StringBuilder();
msg.append("MotionEvent { action=").append(actionToString(getAction()));
final int pointerCount = getPointerCount();
for (int i = 0; i < pointerCount; i++) {
msg.append(", id[").append(i).append("]=").append(getPointerId(i));
msg.append(", x[").append(i).append("]=").append(getX(i));
msg.append(", y[").append(i).append("]=").append(getY(i));
msg.append(", toolType[").append(i).append("]=").append(
toolTypeToString(getToolType(i)));
}
msg.append(", buttonState=").append(MotionEvent.buttonStateToString(getButtonState()));
msg.append(", metaState=").append(KeyEvent.metaStateToString(getMetaState()));
msg.append(", flags=0x").append(Integer.toHexString(getFlags()));
msg.append(", edgeFlags=0x").append(Integer.toHexString(getEdgeFlags()));
msg.append(", pointerCount=").append(pointerCount);
msg.append(", historySize=").append(getHistorySize());
msg.append(", eventTime=").append(getEventTime());
msg.append(", downTime=").append(getDownTime());
msg.append(", deviceId=").append(getDeviceId());
msg.append(", source=0x").append(Integer.toHexString(getSource()));
msg.append(" }");
return msg.toString();
}
如果您在SAME类中打开 actionToString 方法 :
public static String actionToString(int action) {
switch (action) {
case ACTION_DOWN:
return "ACTION_DOWN";
case ACTION_UP:
return "ACTION_UP";
case ACTION_CANCEL:
return "ACTION_CANCEL";
case ACTION_OUTSIDE:
return "ACTION_OUTSIDE";
case ACTION_MOVE:
return "ACTION_MOVE";
case ACTION_HOVER_MOVE:
return "ACTION_HOVER_MOVE";
case ACTION_SCROLL:
return "ACTION_SCROLL";
case ACTION_HOVER_ENTER:
return "ACTION_HOVER_ENTER";
case ACTION_HOVER_EXIT:
return "ACTION_HOVER_EXIT";
}
int index = (action & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT;
switch (action & ACTION_MASK) {
case ACTION_POINTER_DOWN:
return "ACTION_POINTER_DOWN(" + index + ")";
case ACTION_POINTER_UP:
return "ACTION_POINTER_UP(" + index + ")";
default:
return Integer.toString(action);
}
}
但是,当我尝试使用这种方法时
MotionEvent.actionToString(event.getAction);
IDE告诉我有关错误的信息。
Cannot resolve method actionToString(int);
为什么我收到此错误?
链接到类方法:
答案 0 :(得分:1)
此方法存在于早于API 19的平台源中,但隐藏。
请参阅JellyBean的来源:
/**
* Returns a string that represents the symbolic name of the specified action
* such as "ACTION_DOWN", "ACTION_POINTER_DOWN(3)" or an equivalent numeric constant
* such as "35" if unknown.
*
* @param action The action.
* @return The symbolic name of the specified action.
* @hide
*/
public static String actionToString(int action) {
注意JavaDoc中的@hide
注释。
在KitKat中,注释不再存在:
答案 1 :(得分:0)
此方法已添加Android SDK 19 - 4.4:http://developer.android.com/reference/android/view/MotionEvent.html#actionToString%28int%29
您必须将targetSdkVersion设置为最小值才能使用它。一定要使用
@SuppressLint("NewApi")
if (android.os.Build.VERSION.SDK_INT >= 19)
{
//...
}
如果你的min sdk设置在19以下
修改强> 它似乎是在JellyBean的更高版本中秘密添加的: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/view/MotionEvent.java#MotionEvent.actionToString%28int%29