[英]Android - OnTouchListener not working
您好我已經構建了一個具有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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.