有效使用触摸/悬停坐标



在Android手机上,我获得了坐标的信息,用户可以触摸他/她的电话屏幕或设备。然后,我如何使用/传递这些X-Y坐标来运行我的特定功能,并在我的代码中使用它

帖子中的引用,如果用户触摸了您的应用程序的屏幕,则可以获取坐标。在触摸侦听器实现之后,然后在if语句MotionEvent.ACTION_UP中将其放在此处

 float myX = event.getX();
 float myY = event.getY();
 // now you can then pass myX,myY in your method or function as parameters

如果您碰巧针对Api 14+,则可以使用与该帖子中的第一个方法相同的onhover,而不是使用setOnTouchListener使用getWindow().getDecorView().setOnHoverListener()

旁注使用的是FIRS更好,因为无法信任工作,实际上对我不起作用,那只是btw

希望我回答您的问题。

您可以通过扩展Android视图和覆盖OnTouchEvent方法来创建自定义视图。例如,

public class NotTouchableWebView extends WebView {
private Context context;
public NotTouchableWebView(Context context) {
    super(context);
    this.context=context;
}
public NotTouchableWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context=context;
}
public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context=context;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    this.context=context;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (some condition){
        //your specific function
        return false;
    }
    return super.onTouchEvent(event);
}

}

最新更新