我正在使用触摸事件进行输入。MotionEvent.ACTION_DOWN 和 MotionEvent.ACTION_UP 都会在按下期间调用。当我的手指从表面抬起时,它应该只调用 ACTION_UP 。他们同时被召唤。请参阅以下代码和 logcat 输出。
\n\n public boolean onTouchEvent(MotionEvent e) {\n // MotionEvent reports input details from the touch screen\n // and other input controls. In this case, you are only\n // interested in events where the touch position changed.\n\n float x = e.getX();\n float y = e.getY();\n\n switch (e.getAction()) {\n case MotionEvent.ACTION_DOWN:\n mRenderer.scanButtonsDown(x, y);\n\n Log.i("Touch", "Action Down Case");\n\n case MotionEvent.ACTION_UP:\n mRenderer.scanButtonsUp(x, y);\n\n Log.i("Touch", "Action Up Case");\n\n }\n\n mPreviousX = x;\n mPreviousY = y;\n return true;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是 logcat 输出:
\n\n09-01 14:58:24.801 4683-4683/nf.co.av_club.opengl I/Touch\xef\xb9\x95 Action Down Case\n09-01 14:58:24.811 4683-4683/nf.co.av_club.opengl I/Touch\xef\xb9\x95 Action Up Case\n09-01 14:58:24.961 4683-4683/nf.co.av_club.opengl I/Touch\xef\xb9\x95 Action Up Case\nRun Code Online (Sandbox Code Playgroud)\n\n之前的 logcat 在按一次屏幕时会生成一个向下的情况和两个向上的情况。现在我将进行长时间的记者采访。
\n\n09-01 14:58:37.113 4683-4683/nf.co.av_club.opengl I/Touch\xef\xb9\x95 Action Down Case\n09-01 14:58:37.123 4683-4683/nf.co.av_club.opengl I/Touch\xef\xb9\x95 Action Up Case\n09-01 14:58:41.097 4683-4683/nf.co.av_club.opengl I/Touch\xef\xb9\x95 Action Up Case\nRun Code Online (Sandbox Code Playgroud)\n\n看到时间戳了吗?Down 和 Up 同时调用。两秒钟后,当我真正将手指从平板电脑上移开时,它会再次被调用。有人有建议吗?
\n你错过了break;每个结尾处的case。请像下面这样操作。
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
mRenderer.scanButtonsDown(x, y);
Log.i("Touch", "Action Down Case");
break; //add this line
case MotionEvent.ACTION_UP:
mRenderer.scanButtonsUp(x, y);
Log.i("Touch", "Action Up Case");
break; //and this line
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2212 次 |
| 最近记录: |