【问题标题】:how to disable dispatch touch event in some portion of screen如何在屏幕的某些部分禁用调度触摸事件
【发布时间】:2012-10-01 18:46:51
【问题描述】:

为了您的理解,我想在某些区域禁用调度触摸,这是我的屏幕。

您可以在图像中看到页眉、页脚和地图视图,但是当我单击位置按钮(页眉右侧)时,我的地图也会收到我不想要的通知(就像它被触摸一样)。我希望只点击按钮的 onClick 事件而不是调度事件。

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- include title bar for all screen -->

<include
    android:id="@+id/include1"
    layout="@layout/titlebar_layout"
    android:layout_gravity="top" />



<FrameLayout
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.88" 
    >

    <com.google.android.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.63"
        android:apiKey="0VkXbAOFvAq7b6uaGHSmnS2a2VosPxoS6ceHY_g"
        android:clickable="true" >
    </com.google.android.maps.MapView>


</FrameLayout>

<include
    android:id="@+id/include2"
    android:layout_gravity="bottom"
    android:layout_marginBottom="38dp"
    layout="@layout/bottom_layout" />

</LinearLayout>

任何帮助将不胜感激。

【问题讨论】:

    标签: android android-mapview dispatchevent


    【解决方案1】:

    您可以覆盖dispatchTouchEvent,并检查用户触摸屏幕的位置..

      @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {     
        //first check if the location button should handle the touch event
        if(locBtn != null) {
            int[] pos = new int[2];
            locBtn.getLocationOnScreen(pos);
            if(ev.getY() <= (pos[1] + locBtn.getHeight()) && ev.getX() > pos[0]) //location button event
                return locBtn.onTouchEvent(ev);
        }
            return super.dispatchTouchEvent(ev);
    }
    

    【讨论】:

    • 嘿,谢谢您的回答。如果可行,我很快就会回来接受您的回答
    • 嘿努努,你的回答对我真的很有帮助,但我仍然无法获得按钮位置什么是 pos[1] ?如果我在那里做 pos[0],那么即使我触摸其他任何地方,我也会不断收到按钮事件。请帮助我
    • developer.android.com/reference/android/view/…, pos[0] 包含视图的 x,pos[1] 包含 y.. 试试 if(ev.getY() pos[0])
    • OMG 它的工作 SOflow 请允许我为这个答案投票 10 次......这对我来说非常值得非常感谢@Nunu
    • 向如此好的态度致敬
    【解决方案2】:

    MapView 接收dispatchTouchEvent() 事件的原因是Android 开始分发最后添加的视图的事件,如果它没有在那里处理,那么它会被分派到最后一个添加的视图之前,所以上...

    在您的 xml 布局中,您首先添加页眉,然后添加 MapView,最后添加页脚。因此,在您的情况下,事件首先被分派到页脚,然后是 MapView,最后是页眉。

    你必须选择解决这个问题:

    • 最简单的方法是重新排列布局中的项目,从 mapview 开始,然后添加所有其他项目。您可能需要使用相对布局才能正确定位它们。使用此 MapView 将不会看到按钮处理的事件。
    • 保留您的布局并让 MapView 接收事件。 MapView 将需要测试事件是否应该由他处理或忽略。为此,您可以使用@Nunu 建议的代码,它应该可以工作。也许您需要在按钮 Y 坐标中添加按钮高度。

    祝你好运。

    【讨论】:

    • +1 没有人能阻止我按下 Upvote 以获得如此好的解释......非常感谢
    • 我很快就会回来感谢您的回答..:):)
    • @Luis ..再次感谢我遵循您的第二个选项(@Nunu 也建议)但没有工作..:( 将 y 坐标添加到按钮高度是什么意思??谢谢提前
    • 当您调用getLocationOnScreen() 时,您将获得按钮顶部的 Y 坐标。单击按钮时,用户将单击按钮顶部和按钮之间的区域。您可以找到添加按钮高度的按钮 y 坐标。
    【解决方案3】:

    使用相对布局,底部为地图视图,顶部为点击按钮......并为按钮添加点击事件

    【讨论】:

      【解决方案4】:

      使用 view.bringToFront() 将视图位置置于其他视图之上。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-07
        • 2019-06-17
        • 1970-01-01
        相关资源
        最近更新 更多