2014-01-23 56 views
1

在Android SDK中重寫方法toString使用actionToString方法作爲公共靜態修飾符。如果開放的源代碼,你必須看到:爲什麼MotionEvent.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(); 
    } 

如果你打開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);

爲什麼我收到這個錯誤?


鏈接到類方法:

actionToString method

toString method

+0

可能是你不建設分鐘-SDK 19 - 此法剛剛在SDK中補充說:http://developer.android .com/reference/android/view/MotionEvent.html#actionToString%28int%29 –

+0

''在我的清單中。作爲一個附加的sourse - sdk 18. –

+0

將它設置爲19和它將工作 –

回答

1

存在這種方法在API 19以前的平臺的源中,但是隱藏了

看到源從豆形軟糖:

https://android.googlesource.com/platform/frameworks/base/+/jb-mr2.0.0-release/core/java/android/view/MotionEvent.java

/** 
* 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) { 

注意@hide註釋中的JavaDoc。

在奇巧,註釋不再存在:

https://android.googlesource.com/platform/frameworks/base/+/kitkat-release/core/java/android/view/MotionEvent.java

+0

謝謝你的介紹!我會接受的。 –

+0

我有一個問題。隱藏位於JavaDoc中的註釋。它似乎是作爲這種方法的註釋而相關的? –

+0

我不確定你的意思,但看看這個:http://stackoverflow.com/questions/17035271/what-does-hide-mean-in-the-android-source-code – Natix

0

與Android SDK添加了此方法爲19 - 4.4:http://developer.android.com/reference/android/view/MotionEvent.html#actionToString%28int%29

你有你的targetSdkVersion設置爲19最低限度使用它。一定要使用

@SuppressLint("NewApi") 

    if (android.os.Build.VERSION.SDK_INT >= 19) 
    { 
     //... 
    } 

如果你分鐘SDK設置爲低於19

編輯: 它似乎在豆形軟糖的以後版本中偷偷加入: 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

+0

這是不正確的答案。 –