2014-11-16 93 views
0
@Override 
public boolean onTouchEvent(MotionEvent event){ 

    int action = event.getActionMasked(); 
    switch (action) 
    { 
     case (MotionEvent.ACTION_DOWN) : 
      return true; 
     case (MotionEvent.ACTION_MOVE) : 
      return true; 

     default : 
      return super.onTouchEvent(event); 

    int THIS_CODE_WORKS = event.getActionMasked(); 
    String s = ""; 
    switch (THIS_CODE_WORKS) 
    { 
     case (MotionEvent.ACTION_DOWN) : 
      s="down"; 
      Log.v("Action",s); 
      break; 
     case (MotionEvent.ACTION_MOVE) : 

即使調試器顯示動作變量= 2,MotionEvent.ACTION_MOVE(2)的case語句也會被忽略。默認語句總是執行。這是來自Google的示例代碼,但它不起作用。我無法弄清楚爲什麼不按預期評估報表。Android Studio無法識別開關盒值

+0

你確定嗎?嘗試設置一些日誌語句。 – Henry

+0

已更新。修改後的case語句添加了THIS_CODE_WORKS行和下面的行。我想它不喜歡只有一個返回語句。 –

回答

0

嘗試使用getAction()和改變你的開關情況

public boolean onTouchEvent(MotionEvent event) { 
    int action = event.getAction(); 

    switch (action) { 
     case MotionEvent.ACTION_DOWN: 
      // finger touches the screen 
      break; 

     case MotionEvent.ACTION_MOVE: 
      // finger moves on the screen 
      break; 

     case MotionEvent.ACTION_UP: 
      // finger leaves the screen 
      break; 
    } 

    //handled the event and no further processing is required 
    return true; 
} 
相關問題