2014-11-16 95 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; 
} 
相关问题