如何在 Android 上将视图的 onClick 事件传递给其父级? [英] How to pass a view's onClick event to its parent on Android?

查看:28
本文介绍了如何在 Android 上将视图的 onClick 事件传递给其父级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在布局中有一个 TextView,其背景是一个选择器.并且 TextView 的文本设置为 Spanned from HTML.然后我使用 LinkMovementMethod 设置 TextView.

I have a TextView in a layout whos background is a Selector. And the TextView's text is set to Spanned from HTML. Then I set the TextView with the LinkMovementMethod.

现在,当我点击 TextView 时,点击事件不会发送到其父布局以触发选择器.

Now when I tap on the TextView, the click event is not sent to its parent layout to trigger the selector.

应该如何解决?

推荐答案

我认为您需要使用其中一种方法才能在事件被发送到适当的组件之前拦截它:

I think you need to use one of those methods in order to be able to intercept the event before it gets sent to the appropriate components:

Activity.dispatchTouchEvent(MotionEvent) - 这允许您的 Activity 在它们被分派到窗口之前拦截所有触摸事件.

Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.

ViewGroup.onInterceptTouchEvent(MotionEvent) - 这允许 ViewGroup 在事件被分派到子视图时观看它们.

ViewGroup.onInterceptTouchEvent(MotionEvent) - This allows a ViewGroup to watch events as they are dispatched to child Views.

ViewParent.requestDisallowInterceptTouchEvent(boolean) - 在父视图上调用它以指示它不应使用 onInterceptTouchEvent(MotionEvent) 拦截触摸事件.

ViewParent.requestDisallowInterceptTouchEvent(boolean) - Call this upon a parent View to indicate that it should not intercept touch events with onInterceptTouchEvent(MotionEvent).

更多信息此处.

希望有所帮助.

这篇关于如何在 Android 上将视图的 onClick 事件传递给其父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆