Android – OnTouchListener无法正常工作

您好我已经构建了一个具有root relativelayout视图的应用程序.当用户触摸屏幕时我试图设置一个触摸式监听器返回,但它似乎没有触发,我无法弄清楚我哪里出错了.我搜索过并在线查看,但从我看到的每个人似乎都使用与我相同的方法 – 我只是不工作.任何人都明白为什么?

  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" />

解决方法:

把它放在xml文件中的relativelayout的每个子节点:

android:clickable="false"
android:focusable="false"
上一篇:exception ORA-00918: 未明确定义列


下一篇:如何用手指画出多条线? (机器人)