[英]Android - OnTouchListener not working
Hello I've built an app which has a root relativelayout view. 您好我已经构建了一个具有root relativelayout视图的应用程序。 I've tried to set an on touch listener to return when the user has touched the screen but it doesn't seem to firing and I can't figure out where I'm going wrong.
当用户触摸屏幕时我试图设置一个触摸式监听器返回,但它似乎没有触发,我无法弄清楚我哪里出错了。 I've searched and looked online but from what I've seen everyone seems to user the same method as me - mine just doesn't work.
我搜索过并在线查看,但从我看到的每个人似乎都使用与我相同的方法 - 我只是不起作用。
can anyone see why? 谁能明白为什么?
RelativeLayout relativeLayout;
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
//relative layout on touch listener
relativeLayout.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
//down - finger on screen
if (event.getAction()== MotionEvent.ACTION_DOWN) {
//store center of screen lat lon
centerOfScreenLatLng = getScreenCenterLatLon();
toastMsg = "down";
toastShort();
return true;
}
//up - finger off screen
else if (event.getAction() == MotionEvent.ACTION_UP) {
toastMsg = "up";
toastShort();
//get center of screen lat lon
LatLng centerPoint = getScreenCenterLatLon();
//if not equal then screen has been moved
if (centerPoint != centerOfScreenLatLng){
//check which markers are currently in view
checkMarkersInView();
}
return true;
}
return false;
}
});
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_1"
tools:context=".MainActivity"
android:clickable="true"
android:focusable="true" />
put this for every child of relativelayout in xml file: 把它放在xml文件中的relativelayout的每个子节点:
android:clickable="false"
android:focusable="false"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.