在Android开发中,我们经常会对一个View设置onClick,onLongClick,onTouch事件,有时还会同时设置这三个事件,那么在同时设置这三个时候,执行顺序是什么样呢?
首先,官方文档上面对onLongClick()和onTouch()的描述如下:
onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners.
大致意思就是:onLongClick()方法会返回一个boolean值,如果是true,表示已经处理了此事件,不需要继续传递;如果是false,表示没有处理,事件会继续到其他的监听事件。
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
大致意思是:onTouch()方法,会包含多个行为,ACITON_DOWN,ACTION_MOVE,ACTION_UP。如果返回值时true,就表示会处理这个事件,就不会再调用onClick或者onLongClick,如果返回false,则会继续调用onLongClick。
写个简单的测试程序:
package com.qwei.demo; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.View.OnTouchListener; import android.widget.TextView; public class MainActivity extends Activity { private final static String TAG = MainActivity.class.getName(); private TextView clickMe,showLog; private String log; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); clickMe = (TextView) findViewById(R.id.click_me); showLog = (TextView) findViewById(R.id.show_log); clickMe.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View arg0) { // TODO Auto-generated method stub Log.d("TAG", "onLongClick is called............."); log = log +"onLongClick is called.............\n"; showLog.setText(log); return false; } }); clickMe.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d("TAG", "onClick is called............."); log = log +"onClick is called.............\n"; showLog.setText(log); } }); clickMe.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { //Log.d("TAG", "onTouch is called............."); switch (arg1.getAction()) { case MotionEvent.ACTION_DOWN: Log.d("TAG", "ACTION_DOWN............."); log = log +"oACTION_DOWN............\n"; showLog.setText(log); break; case MotionEvent.ACTION_MOVE: Log.d("TAG", "ACTION_MOVE............."); log = log +"ACTION_MOVE.............\n"; showLog.setText(log); break; case MotionEvent.ACTION_CANCEL: Log.d("TAG", "ACTION_CANCEL............."); log = log +"ACTION_CANCEL............\n"; showLog.setText(log); break; case MotionEvent.ACTION_UP: Log.d("TAG", "ACTION_UP............."); log = log +"ACTION_UP............\n"; showLog.setText(log); break; default: break; } return false; } }); } }
当onTouch返回false,onLongClick也返回false,长按view时,会调用onTouch,一段时间之后调用onLongClick,抬起手指的时候调用onClick,如下图所示:
<img src="http://img.blog.csdn.net/20150510163553660?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVsbG8wMzcw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="423" height="641" alt="" />
当onTouch返回false,onLongClick也返回true,长按view时,会调用onTouch,一段时间之后调用onLongClick,抬起手指的时候不会调用onClick,如下图:
当onTouch返回true,长按view时,会调用onTouch,onLongClick和onClick都不会被调用,如下图:
时间: 2024-10-10 20:49:37