android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解。
false ,则交给这个 view 的 interceptTouchEvent 方法来决定是否要拦截这个事件,如果 interceptTouchEvent 返回 true ,也就是拦截掉了,则交给它的 onTouchEvent 来处理,如果 interceptTouchEvent 返回 false ,那么就传递给子 view ,由子 view 的 dispatchTouchEvent 再来开始这个事件的分发。如果事件传递到某一层的子 view 的 onTouchEvent 上了,这个方法返回了 false
,那么这个事件会从这个 view 往上传递,都是 onTouchEvent 来接收。而如果传递到最上面的 onTouchEvent 也返回 false 的话,这个事件就会“消失”,而且接收不到下一次事件。
<test.lzqdiy.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<test.lzqdiy.MyTextView
android:layout_width="200px"
android:layout_height="200px"
android:id="@+id/tv"
android:text="lzqdiy"
android:textSize="40sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:textColor="#0000FF"/>
</test.lzqdiy.MyLinearLayout>
import android.os.Bundle; public class TestTouchEventApp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout; public class MyLinearLayout extends LinearLayout {
private final String TAG = "MyLinearLayout";
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
Log.d(TAG, TAG);
}
@Override
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG,
"dispatchTouchEvent action:ACTION_DOWN");break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,
"dispatchTouchEvent action:ACTION_MOVE");break;
case MotionEvent.ACTION_UP:
Log.d(TAG,
"dispatchTouchEvent action:ACTION_UP");break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG,
"dispatchTouchEvent action:ACTION_CANCEL");break;
}
}
@Override
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG,
"onInterceptTouchEvent action:ACTION_DOWN");break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,
"onInterceptTouchEvent action:ACTION_MOVE");break;
case MotionEvent.ACTION_UP:
Log.d(TAG,
"onInterceptTouchEvent action:ACTION_UP");break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG,
"onInterceptTouchEvent action:ACTION_CANCEL");break;
}
return false;
}
@Override
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG,
"---onTouchEvent action:ACTION_DOWN");break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,
"---onTouchEvent action:ACTION_MOVE");break;
case MotionEvent.ACTION_UP:
Log.d(TAG,
"---onTouchEvent action:ACTION_UP");break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG,
"---onTouchEvent action:ACTION_CANCEL");break;
}
return true;
}
}
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView; public class MyTextView extends TextView {
private final String TAG = "MyTextView";
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG,
"dispatchTouchEvent action:ACTION_DOWN");break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,
"dispatchTouchEvent action:ACTION_MOVE");break;
case MotionEvent.ACTION_UP:
Log.d(TAG,
"dispatchTouchEvent action:ACTION_UP");break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG,
"onTouchEvent action:ACTION_CANCEL");break;
}
}
@Override
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG,
"---onTouchEvent action:ACTION_DOWN");break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,
"---onTouchEvent action:ACTION_MOVE");break;
case MotionEvent.ACTION_UP:
Log.d(TAG,
"---onTouchEvent action:ACTION_UP");break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG,
"---onTouchEvent action:ACTION_CANCEL");break;
}
return true;
}
}