Android - OnTouchListener 不工作
Android - OnTouchListener not working
您好,我构建了一个具有根相对布局视图的应用程序。当用户触摸屏幕时,我尝试将触摸侦听器设置为 return,但它似乎没有触发,我不知道哪里出错了。我在网上搜索和查看,但据我所见,似乎每个人都使用与我相同的方法 - 我的就是行不通。
谁能看出为什么?
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"
您好,我构建了一个具有根相对布局视图的应用程序。当用户触摸屏幕时,我尝试将触摸侦听器设置为 return,但它似乎没有触发,我不知道哪里出错了。我在网上搜索和查看,但据我所见,似乎每个人都使用与我相同的方法 - 我的就是行不通。
谁能看出为什么?
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"