3

你好我已經建立了一個應用程序,它有一個根相對佈局視圖。我試圖設置一個觸摸監聽器,當用戶觸摸屏幕時返回,但它似乎並沒有發射,我不知道我要去哪裏錯了。我已經搜索並在線看,但從我看到每個人似乎用戶採用了與我一樣的方法 - 我的工作不起作用。
任何人都可以看到爲什麼?Android - OnTouchListener不工作

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

什麼是'toastShort()'? – Psytho

+0

只是一種在屏幕上顯示吐司消息的方法 –

+0

是的,名稱是自我描述的:)但它在哪裏定義?我沒有找到它的參考,所以這是你自己的方法。 – Psytho

回答

6

把這個在XML文件中的RelativeLayout的每一個孩子:

android:clickable="false" 
android:focusable="false" 
+0

如果元素需要點擊也會怎麼樣? –

+0

您可以放置​​兩個相對佈局,然後將第二個相關佈局中您想要單擊的視圖放到正確的位置 –