android之OnTouchListener只能监听到ACTION_DOWN-----onTouchListener的返回值问题

来源:互联网 发布:德阳数控铣床编程培训 编辑:程序博客网 时间:2024/06/08 12:06

做这样一个效果,界面上显示一个紫色方块,任意拖动方块到指定位置都可以,

运行后结果发现方块怎么都拖不动,打印log只有ACTION_DOWN有反应,MOVE和UP都监听不到,

很是奇怪,先把整段代码都贴下面了

package jason.com.security.ui;import jason.com.security.R;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.view.Window;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.RelativeLayout.LayoutParams;public class DragViewActivity extends Activity implements OnTouchListener {String TAG = "DragViewActivity";void log(String s) {Log.d(TAG, s);}private ImageView iv_drag_location;private SharedPreferences sp;int startX;int startY;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.drag_view);sp = getSharedPreferences("config", MODE_PRIVATE);iv_drag_location = (ImageView) findViewById(R.id.iv_drag_location);iv_drag_location.setOnTouchListener(this);}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();int x = sp.getInt("lastX", 0);int y = sp.getInt("lastY", 0);log("last X is " + x + ",lastY is " + y);RelativeLayout.LayoutParams params = (LayoutParams) iv_drag_location.getLayoutParams();params.leftMargin = x;params.topMargin = y;iv_drag_location.setLayoutParams(params);}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.iv_drag_location:switch (event.getAction()) {case MotionEvent.ACTION_DOWN:startX = (int) event.getRawX();startY = (int) event.getRawY();log("ACTION_DOWN :" + startX + "-" + startY);break;case MotionEvent.ACTION_MOVE:int dx = (int) (event.getRawX() - startX);int dy = (int) (event.getRawY() - startY);log("ACTION_MOVE :" + dx + "-" + dy);int l = iv_drag_location.getLeft();int r = iv_drag_location.getRight();int t = iv_drag_location.getTop();int b = iv_drag_location.getBottom();iv_drag_location.layout(l + dx, t + dy, r + dx, b + dy);startX = (int) event.getRawX();startY = (int) event.getRawY();break;case MotionEvent.ACTION_UP:int lastX = iv_drag_location.getLeft();int lastY = iv_drag_location.getTop();log("ACTION_UP :" + lastX + "-" + lastY);Editor editor = sp.edit();editor.putInt("lastX", lastX);editor.putInt("lastY", lastY);editor.commit();break;default:break;}break;default:break;}return false;}}


检查了N遍,逻辑上实在没问题,

最后发现,最后的返回值是return false;

由于这个是编译器自动生成的,我也没改动过,就改成了return true试试

果然,就是这个的问题,改成return true之后就正常了,

文档中对该方法的返回值描述如下:True if the listener has consumed the event, false otherwise。

大概意思就是说,如果返回true,则表示监听器处理了该事件(我的理解就是不用继续向上传递该事件了,该事件的传递到此为止);

否则返回false。

这样一想,我觉得之前返回false,可能造成之后检测到的事件向上传递了,而没有在该监听函数中处理。

所以导致了错误。

 

作者:jason0539

微博:http://weibo.com/2553717707

博客:http://blog.csdn.net/jason0539(转载请说明出处)

4 0
原创粉丝点击