onInterceptTouchEvent() 与 onTouch() 事件分析

简介: onInterceptTouchEvent() 与 onTouch() 事件分析

学习笔记:直接上代码,对了在这里强调一点 onTouch() 与 onTouchEvent() 事件不一样。

先看布局文件:

<?xml version="1.0" encoding="utf-8"?>
<com.tinno.intercepttouch.MyFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/tv"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#000000"
        android:text="Hello World!"
       />
</com.tinno.intercepttouch.MyFrameLayout>

MyFrameLayout 是一个自定义View:

public class MyFrameLayout extends FrameLayout  {
    public MyFrameLayout(@NonNull Context context) {
        super(context);
    }
    public MyFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    public MyFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                System.out.println("----onInterceptTouchEvent---ACTION_DOWN");
                return true;
           //     break;
            case MotionEvent.ACTION_POINTER_UP:
                System.out.println("----onInterceptTouchEvent---ACTION_POINTER_UP");
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                System.out.println("----onInterceptTouchEvent---ACTION_POINTER_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                System.out.println("----onInterceptTouchEvent---ACTION_MOVE");
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                System.out.println("----onInterceptTouchEvent---ACTION_UP");
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + event.getActionMasked());
        }
        return false;
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity  {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.tv);
        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        System.out.println("----onTouch---ACTION_DOWN");
                    break;
                    case MotionEvent.ACTION_POINTER_UP:
                        System.out.println("----onTouch---ACTION_POINTER_UP");
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        System.out.println("----onTouch---ACTION_POINTER_DOWN");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        System.out.println("----onTouch---ACTION_MOVE");
                        break;
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                        System.out.println("----onTouch---ACTION_UP");
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + motionEvent.getActionMasked());
                }
                return true;
            }
        });
    }
}

好了,开始进行分析:


image.png


当 onInterceptTouchEvent 事件返回 true,ViewGroup会将该事件进行拦截,无法向下(View)传递。在  onTouch 中将收不到事件。


当 onTouch  事件返回 true,则表明事件不再向下传递,自己处理,消耗掉,例子:该view的 onClick 事件将会失效。


ViewGroup事件传递总结

image.png

View事件传递总结


image.png



这里需要特别注意的是,onTouch()的执行 先于onClick()。

相关文章
|
小程序 前端开发
uniapp 小程序接入友盟 ,自定义事件携带参数
uniapp 小程序接入友盟 ,自定义事件携带参数
700 0
|
网络协议 开发工具 git
解决 git 报错 “fatal: unable to access ‘https://github.com/.../.git‘: Recv failure Connection was rese
在使用 Git/Git小乌龟 进行代码管理的过程中,经常会遇到各种各样的问题,其中之一就是在执行 git clone 或 git pull 等操作时出现 “fatal: unable to access ‘https://github.com/…/.git’: Recv failure Connection was reset” 的报错。这个问题通常是由网络连接问题或代理设置不正确导致的。在我的个人使用经验中,我亲自尝试了四种方法,它们都能够有效地解决这个报错。个人比较推荐方法二。
11207 1
|
7天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16760 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
1天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
6天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1199 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2274 7