Java 使用motionevents模拟鼠标单击

Java 使用motionevents模拟鼠标单击,java,android-studio,Java,Android Studio,我试图模拟当应用程序中的鼠标垫被按下而未移动时的鼠标点击 if(isConnected && out!=null){ switch(event.getAction()){ case MotionEvent.ACTION_DOWN: //save X and Y positions when user touches the TextView

我试图模拟当应用程序中的鼠标垫被按下而未移动时的鼠标点击

  if(isConnected && out!=null){
                switch(event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        //save X and Y positions when user touches the TextView
                        initX =event.getX();
                        initY =event.getY();
                        mouseMoved=false;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        disX = event.getX()- initX; //Mouse movement in x direction
                        disY = event.getY()- initY; //Mouse movement in y direction
                        /*set init to new position so that continuous mouse movement
                        is captured*/
                        initX = event.getX();
                        initY = event.getY();
                        if(disX !=0|| disY !=0){
                            out.println(disX +","+ disY); //send mouse movement to server
                        }
                        mouseMoved=true;
                        break;
                    case MotionEvent.ACTION_UP:
                        //consider a tap only if usr did not move mouse after ACTION_DOWN
                        if(!mouseMoved){
                               out.println(Constants.MOUSE_LEFT_CLICK);
                        }
                }
            }
            return true;
        }
    });
}

我试过这个,但我不明白为什么它不起作用。每次我点击鼠标垫,鼠标就会移动。如何解决这个问题?

如果是else,请尝试使用
if-else
,然后将按压放在第一位,将移动放在else的情况下。

我通过设置边距来解决问题

int max = 2 
int min = -2
case MotionEvent.ACTION_UP:
                        //consider a tap only if usr did not move mouse after ACTION_DOWN
                        if(disX <= max && disX >= min && disY <= max && disY >= min){
                            out.println(Constants.MOUSE_LEFT_CLICK);
                            mouseMoved=false;
                            break;
                        }
                }
            }
            return true;
        }
int max=2
int min=-2
case MotionEvent.ACTION\u UP:
//仅当usr在下一操作后未移动鼠标时,才考虑轻触
if(disX=min&&disY=min){
out.println(常量。鼠标左键单击);
mouseMoved=false;
打破
}
}
}
返回true;
}