package com.domo.touch;  import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView;  public class TestTouchActivity extends Activity {     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          TextView textView = (TextView) findViewById(R.id.textview);         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 false;//注意:return false             }         });     }      @Override     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 super.dispatchTouchEvent(ev);//注意     }  }
代码图如上:
dispatchTouchEvent的返回值为super.dispatchTouchEvent(ev)时,获取到super.dispatchTouchEvent(ev)为False;点击Textview的显示的结果为

04-28 01:09:32.978: V/TAG(5733): Activity_dis: ACTION_DOWN0 04-28 01:09:32.978: V/TAG(5733): TextView: ACTION_DOWN0 04-28 01:09:32.978: V/TAG(5733): Activity_dis: false 04-28 01:09:32.978: V/TAG(5733): TextView: ACTION_DOWN0     //TextView只能获取到Down事件 04-28 01:09:33.068: V/TAG(5733): Activity_dis: false 04-28 01:09:33.088: V/TAG(5733): Activity_dis: ACTION_UP1 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_DOWN02 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: false4 04-28 01:14:25.571: V/TAG(5884): Activity_dis: ACTION_UP15 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_DOWN02 04-28 01:17:00.398: V/TAG(5995): TextView: ACTION_DOWN0               // 效果没见发生变化3 04-28 01:17:00.398: V/TAG(5995): Activity_dis: false4 04-28 01:17:00.418: V/TAG(5995): Activity_dis: ACTION_UP15 04-28 01:17:00.418: V/TAG(5995): Activity_dis: false
下面在 Activity中添加 onThouchEvent事件详细代码见下面

package com.domo.touch;  import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView;  public class TestTouchActivity extends Activity {     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          TextView textView = (TextView) findViewById(R.id.textview);         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 false;             }         });     }      @Override     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 true;//注意     }      @Override     public boolean onTouchEvent(MotionEvent event) {//在Activity中添加了onTouchEvent事件         // TODO Auto-generated method stub         switch (event.getAction()) {         case MotionEvent.ACTION_DOWN:             Log.v("TAG", "onTouchEvent: ACTION_DOWN" + MotionEvent.ACTION_DOWN);             break;         case MotionEvent.ACTION_UP:             Log.v("TAG", "onTouchEvent: ACTION_UP" + MotionEvent.ACTION_UP);             break;         default:             break;         }         Log.v("TAG", "onTouchEvent: " + super.onTouchEvent(event));         return super.onTouchEvent(event);//注意     }      }
点击Textview结果:

04-28 01:22:11.957: V/TAG(6152): Activity_dis: ACTION_DOWN004-28 01:22:11.957: V/TAG(6152): TextView: ACTION_DOWN0       //还是只有一次04-28 01:22:11.957: V/TAG(6152): onTouchEvent: ACTION_DOWN0          04-28 01:22:11.957: V/TAG(6152): onTouchEvent: false04-28 01:22:11.957: V/TAG(6152): Activity_dis: false04-28 01:22:12.047: V/TAG(6152): Activity_dis: ACTION_UP104-28 01:22:12.047: V/TAG(6152): onTouchEvent: ACTION_UP104-28 01:22:12.058: V/TAG(6152): onTouchEvent: false04-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_DOWN004-28 01:25:17.378: V/TAG(6152): TextView: ACTION_DOWN0          //依然只有一次04-28 01:25:17.378: V/TAG(6152): onTouchEvent: ACTION_DOWN004-28 01:25:17.378: V/TAG(6152): onTouchEvent: false04-28 01:25:17.378: V/TAG(6152): Activity_dis: false04-28 01:25:17.468: V/TAG(6152): Activity_dis: ACTION_UP104-28 01:25:17.468: V/TAG(6152): onTouchEvent: ACTION_UP104-28 01:25:17.468: V/TAG(6152): onTouchEvent: false04-28 01:25:17.468: V/TAG(6152): Activity_dis: false
再把public boolean dispatchTouchEvent(MotionEvent ev) { 也返回 returnfalse;

04-28 01:27:16.178: V/TAG(6304): Activity_dis: ACTION_DOWN004-28 01:27:16.178: V/TAG(6304): TextView: ACTION_DOWN0          //依然只有一次04-28 01:27:16.178: V/TAG(6304): onTouchEvent: ACTION_DOWN004-28 01:27:16.178: V/TAG(6304): onTouchEvent: false04-28 01:27:16.178: V/TAG(6304): Activity_dis: false04-28 01:27:16.298: V/TAG(6304): Activity_dis: ACTION_UP104-28 01:27:16.298: V/TAG(6304): onTouchEvent: ACTION_UP104-28 01:27:16.298: V/TAG(6304): onTouchEvent: false04-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; //注意
把这里改成true Textview才能监听到 TextView: ACTION_DOWN0及TextView : ACTION_UP1的动作。
04-28 01:36:26.057: V/TAG(6628): Activity_dis: ACTION_DOWN004-28 01:36:26.057: V/TAG(6628): TextView: ACTION_DOWN004-28 01:36:26.057: V/TAG(6628): Activity_dis: true04-28 01:36:26.148: V/TAG(6628): Activity_dis: ACTION_UP104-28 01:36:26.148: V/TAG(6628): TextView : ACTION_UP1//监听成功04-28 01:36:26.148: V/TAG(6628): Activity_dis: true





更多相关文章

  1. Android(安卓)CheckBox事件setOnCheckedChangeListener
  2. Android(安卓)按钮点击事件
  3. android ListView详解(持续中。。。)
  4. Android4.2 4.4keyguard锁屏流程梳理
  5. Android(安卓)view点击放大缩小
  6. Android(安卓)点击输入框弹出日历 《H》
  7. xml解析,基于XmlPullParser
  8. Android安卓开发 带删除按钮的EditText
  9. 按返回键返回到主界面

随机推荐

  1. Android稳定性测试工具Monkey的使用
  2. Calculator-Android
  3. android java 执行shell命令(笔记)
  4. Android学习 数据存储之_文件存储
  5. [置顶] 通过SIM卡获取GPS,android基站定位
  6. Getting Android Sensor Events While Th
  7. [置顶] android adb adbd analyse
  8. android studio 接入androidannotations
  9. [Android GMS 认证] CTS 问题列表之 CtsS
  10. Android通知栏图标显示网络图片