状态1:由center处理Touch事件
Xml如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <dk.touch.MyLayout android:id="@+id/out" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:background="#ff345600" > <dk.touch.MyLayout android:id="@+id/middle" android:layout_width="200dp" android:layout_height="200dp" android:gravity="center" android:background="#ff885678" > <dk.touch.MyLayout android:id="@+id/center" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff345678" android:focusable="true" android:focusableInTouchMode="true" android:clickable="true" > </dk.touch.MyLayout> </dk.touch.MyLayout> </dk.touch.MyLayout> </LinearLayout> 注意:只有center这个部分是会处理/消费 Touch事件。
Xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<dk.touch.MyLayout
android:id="@+id/out"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#ff345600"
>
<dk.touch.MyLayout
android:id="@+id/middle"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:background="#ff885678"
>
<dk.touch.MyLayout
android:id="@+id/center"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#ff345678"
>
</dk.touch.MyLayout>
</dk.touch.MyLayout>
</dk.touch.MyLayout>
</LinearLayout>
轻触center部分logcat输出结果
源代码: package dk.touch; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyLayout out=(MyLayout) findViewById(R.id.out); out.setName("out"); MyLayout middle=(MyLayout) findViewById(R.id.middle); middle.setName("middle"); MyLayout center=(MyLayout) findViewById(R.id.center); center.setName("center"); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action=ev.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println("Activity"+"|"+actionName+":dispatchTouchEvent"); return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { int action=event.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println("Activity"+"|"+actionName+":onTouchEvent"); return super.onTouchEvent(event); } } package dk.touch; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.LinearLayout; public class MyLayout extends LinearLayout { private String name=""; public MyLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent event) { int action=event.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println(name+"|"+actionName+":onTouchEvent"); return super.onTouchEvent(event); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action=ev.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println(name+"|"+actionName+":dispatchTouchEvent"); return super.dispatchTouchEvent(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { int action=ev.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println(name+"|"+actionName+":onInterceptTouchEvent"); return super.onInterceptTouchEvent(ev); } public String getName() { return name; } public void setName(String name) { this.name = name; } }