【问题标题】:Handling onTouch event in a custom container, with TextView as content在自定义容器中处理 onTouch 事件,以 TextView 作为内容
【发布时间】:2015-02-23 03:38:49
【问题描述】:

我有以下问题。

在 Android 应用程序中,有一个自定义布局类,它扩展了 FrameLayout。它覆盖自定义实现的 onTouchEvent 方法。我们有一个 TextView 作为这个布局类的内容。我们希望这个 TextView 中的 URL 链接是可点击的。为此,我们将 'android:autoLink="web"' 添加到 TextView 的属性中。以下 Xml 随之而来:

            <PinchZoomScrollView
                  android:layout_weight="350"
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:id="@+id/newsDetailScrollView">

                <TextView 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:autoLink="web"
                    android:id="@+id/newsDetailText"/>

            </PinchZoomScrollView>

添加autoLink="web"后,似乎没有调用自定义布局类“PinchZoomEvent”的onTouchEvent。似乎具有 autoLink 属性的 TextView 以某种方式阻止了触摸事件。这很奇怪,因为 PinchZoomScrollView 是 TextView 的父级,它应该首先接收事件。在这种情况下应该怎么做?

【问题讨论】:

    标签: android android-layout textview touch-event


    【解决方案1】:

    在 Android 中,父母不会自动首先接收触摸。您正在考虑其他框架。触摸事件从孩子开始,并在 Android 中沿着链条向上发展。如果您有一个父类(例如滚动器)想要拦截触摸事件并可能覆盖子行为,则它需要实现 onInterceptTouchEvent,并在检测到它想要从子级执行的操作时从它返回 true。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多