Android 各View的Touch事件
如果所示布局一个Layout跟Textview:
1 package com.domo.touch; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.MotionEvent; 7 import android.view.View; 8 import android.view.View.OnTouchListener; 9 import android.widget.TextView; 10 11 public class TestTouchActivity extends Activity { 12 /** Called when the activity is first created. */ 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.main); 17 18 TextView textView = (TextView) findViewById(R.id.textview); 19 textView.setOnTouchListener(new OnTouchListener() { 20 21 public boolean onTouch(View v, MotionEvent event) { 22 // TODO Auto-generated method stub 23 switch (event.getAction()) { 24 case MotionEvent.ACTION_DOWN: 25 Log.v("TAG", "TextView: ACTION_DOWN" + MotionEvent.ACTION_DOWN); 26 break; 27 case MotionEvent.ACTION_UP: 28 Log.v("TAG", "TextView : ACTION_UP" + MotionEvent.ACTION_UP); 29 break; 30 default: 31 break; 32 } 33 34 return false; 35 } 36 }); 37 } 38 39 @Override 40 public boolean dispatchTouchEvent(MotionEvent ev) { 41 // TODO Auto-generated method stub 42 switch (ev.getAction()) { 43 case MotionEvent.ACTION_DOWN: 44 Log.v("TAG", "Activity_dis: ACTION_DOWN" + MotionEvent.ACTION_DOWN); 45 break; 46 case MotionEvent.ACTION_UP: 47 Log.v("TAG", "Activity_dis: ACTION_UP" + MotionEvent.ACTION_UP); 48 break; 49 default: 50 break; 51 } 52 Log.v("TAG", "Activity_dis: " + super.dispatchTouchEvent(ev)); 53 return super.dispatchTouchEvent(ev); 54 } 55 56 }
代码图如上:
当dispatchTouchEvent的返回值为super.dispatchTouchEvent(ev)时,获取到super.dispatchTouchEvent(ev)为False;点击Textview的显示的结果为:
1 04-28 01:09:32.978: V/TAG(5733): Activity_dis: ACTION_DOWN0 2 04-28 01:09:32.978: V/TAG(5733): TextView: ACTION_DOWN0 3 04-28 01:09:32.978: V/TAG(5733): Activity_dis: false 4 04-28 01:09:32.978: V/TAG(5733): TextView: ACTION_DOWN0 TextView只能获取到Down事件 5 04-28 01:09:33.068: V/TAG(5733): Activity_dis: false 6 04-28 01:09:33.088: V/TAG(5733): Activity_dis: ACTION_UP1 7 04-28 01:09:33.088: V/TAG(5733): Activity_dis: false
当dispatchTouchEvent的返回值为false时;点击Textview的显示的结果为:
public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: Log.v("TAG", "Activity_dis: ACTION_DOWN" + MotionEvent.ACTION_DOWN); break; case MotionEvent.ACTION_UP: Log.v("TAG", "Activity_dis: ACTION_UP" + MotionEvent.ACTION_UP); break; default: break; } Log.v("TAG", "Activity_dis: " + super.dispatchTouchEvent(ev)); return false; //即这里改为False时 }
1 04-28 01:14:25.467: V/TAG(5884): Activity_dis: ACTION_DOWN0 2 04-28 01:14:25.467: V/TAG(5884): TextView: ACTION_DOWN0 只能获取到一次Down事件 3 04-28 01:14:25.467: V/TAG(5884): Activity_dis: false 4 04-28 01:14:25.571: V/TAG(5884): Activity_dis: ACTION_UP1 5 04-28 01:14:25.571: V/TAG(5884): Activity_dis: false
当dispatchTouchEvent的返回值为true时;点击Textview的显示的结果为:
1 04-28 01:17:00.398: V/TAG(5995): Activity_dis: ACTION_DOWN0 2 04-28 01:17:00.398: V/TAG(5995): TextView: ACTION_DOWN0 效果没见发生变化 3 04-28 01:17:00.398: V/TAG(5995): Activity_dis: false 4 04-28 01:17:00.418: V/TAG(5995): Activity_dis: ACTION_UP1 5 04-28 01:17:00.418: V/TAG(5995): Activity_dis: false
下面在Activity中添加onThouchEvent事件详细代码见下面:
1 package com.domo.touch; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.MotionEvent; 7 import android.view.View; 8 import android.view.View.OnTouchListener; 9 import android.widget.TextView; 10 11 public class TestTouchActivity extends Activity { 12 /** Called when the activity is first created. */ 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.main); 17 18 TextView textView = (TextView) findViewById(R.id.textview); 19 textView.setOnTouchListener(new OnTouchListener() { 20 21 public boolean onTouch(View v, MotionEvent event) { 22 // TODO Auto-generated method stub 23 switch (event.getAction()) { 24 case MotionEvent.ACTION_DOWN: 25 Log.v("TAG", "TextView: ACTION_DOWN" + MotionEvent.ACTION_DOWN); 26 break; 27 case MotionEvent.ACTION_UP: 28 Log.v("TAG", "TextView : ACTION_UP" + MotionEvent.ACTION_UP); 29 break; 30 default: 31 break; 32 } 33 34 return false; 35 } 36 }); 37 } 38 39 @Override 40 public boolean dispatchTouchEvent(MotionEvent ev) { 41 // TODO Auto-generated method stub 42 switch (ev.getAction()) { 43 case MotionEvent.ACTION_DOWN: 44 Log.v("TAG", "Activity_dis: ACTION_DOWN" + MotionEvent.ACTION_DOWN); 45 break; 46 case MotionEvent.ACTION_UP: 47 Log.v("TAG", "Activity_dis: ACTION_UP" + MotionEvent.ACTION_UP); 48 break; 49 default: 50 break; 51 } 52 Log.v("TAG", "Activity_dis: " + super.dispatchTouchEvent(ev)); 53 return true; 54 } 55 56 @Override 57 public boolean onTouchEvent(MotionEvent event) { 58 // TODO Auto-generated method stub 59 switch (event.getAction()) { 60 case MotionEvent.ACTION_DOWN: 61 Log.v("TAG", "onTouchEvent: ACTION_DOWN" + MotionEvent.ACTION_DOWN); 62 break; 63 case MotionEvent.ACTION_UP: 64 Log.v("TAG", "onTouchEvent: ACTION_UP" + MotionEvent.ACTION_UP); 65 break; 66 default: 67 break; 68 } 69 Log.v("TAG", "onTouchEvent: " + super.onTouchEvent(event)); 70 return super.onTouchEvent(event); 71 } 72 73 }
点击Textview结果:
1 04-28 01:22:11.957: V/TAG(6152): Activity_dis: ACTION_DOWN0 2 04-28 01:22:11.957: V/TAG(6152): TextView: ACTION_DOWN0 3 04-28 01:22:11.957: V/TAG(6152): onTouchEvent: ACTION_DOWN0 还是只有一次 4 04-28 01:22:11.957: V/TAG(6152): onTouchEvent: false 5 04-28 01:22:11.957: V/TAG(6152): Activity_dis: false 6 04-28 01:22:12.047: V/TAG(6152): Activity_dis: ACTION_UP1 7 04-28 01:22:12.047: V/TAG(6152): onTouchEvent: ACTION_UP1 8 04-28 01:22:12.058: V/TAG(6152): onTouchEvent: false 9 04-28 01:22:12.058: V/TAG(6152): Activity_dis: false
把return super.onTouchEvent(event);改为 return false;
点击Textview结果:
04-28 01:25:17.378: V/TAG(6152): Activity_dis: ACTION_DOWN0
04-28 01:25:17.378: V/TAG(6152): TextView: ACTION_DOWN0 依然只有一次
04-28 01:25:17.378: V/TAG(6152): onTouchEvent: ACTION_DOWN0
04-28 01:25:17.378: V/TAG(6152): onTouchEvent: false
04-28 01:25:17.378: V/TAG(6152): Activity_dis: false
04-28 01:25:17.468: V/TAG(6152): Activity_dis: ACTION_UP1
04-28 01:25:17.468: V/TAG(6152): onTouchEvent: ACTION_UP1
04-28 01:25:17.468: V/TAG(6152): onTouchEvent: false
04-28 01:25:17.468: V/TAG(6152): Activity_dis: false
再把public boolean dispatchTouchEvent(MotionEvent ev) { 也返回 return false;
04-28 01:27:16.178: V/TAG(6304): Activity_dis: ACTION_DOWN0
04-28 01:27:16.178: V/TAG(6304): TextView: ACTION_DOWN0 依然只有一次
04-28 01:27:16.178: V/TAG(6304): onTouchEvent: ACTION_DOWN0
04-28 01:27:16.178: V/TAG(6304): onTouchEvent: false
04-28 01:27:16.178: V/TAG(6304): Activity_dis: false
04-28 01:27:16.298: V/TAG(6304): Activity_dis: ACTION_UP1
04-28 01:27:16.298: V/TAG(6304): onTouchEvent: ACTION_UP1
04-28 01:27:16.298: V/TAG(6304): onTouchEvent: false
04-28 01:27:16.298: V/TAG(6304): Activity_dis: false
textView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.v("TAG", "TextView: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
break;
case MotionEvent.ACTION_UP:
Log.v("TAG", "TextView : ACTION_UP" + MotionEvent.ACTION_UP);
break;
default:
break;
}
return true; 把这里改成ture Textview才能监听到 TextView: ACTION_DOWN0及TextView : ACTION_UP1的动作。
04-28 01:36:26.057: V/TAG(6628): Activity_dis: ACTION_DOWN0
04-28 01:36:26.057: V/TAG(6628): TextView: ACTION_DOWN0
04-28 01:36:26.057: V/TAG(6628): Activity_dis: true
04-28 01:36:26.148: V/TAG(6628): Activity_dis: ACTION_UP1
04-28 01:36:26.148: V/TAG(6628): TextView : ACTION_UP1
04-28 01:36:26.148: V/TAG(6628): Activity_dis: true
详细也可以看下这
http://www.apkbus.com/forum.php?mod=viewthread&tid=44296
总结: