[Android:如果触摸了父母,触摸事件将不会到达孩子

问题描述 投票:1回答:1

我有一个由线性布局形成的活动,其中四个按钮作为子元素;布局覆盖整个屏幕尺寸,按钮仅覆盖一半的屏幕尺寸(see here)。

[每个按钮都有触摸侦听器,因此我能够同时处理所有按钮的触摸事件:例如,如果我触摸并按住按钮1,而在触摸按钮2之后,则即使发生按钮2 onTouch事件,即使按钮1未被释放。

当我触摸并按住背景(线性布局),并且同时触摸其中一个按钮时,就会出现问题:onTouch事件未到达按钮;我必须释放背景上的触摸才能使按钮正常工作。

我需要用左手握住手机并用右手按下按钮。

如何使它起作用?

编辑。如下面建议的更多详细信息。

活动代码,将侦听器分配给按钮。侦听器代码无关紧要,在糟糕的情况下,根本无法使用onTouch方法。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_keyboard);

    // Setup keyboard listener
    KeyListener keyListener = new KeyListener();
    findViewById(R.id.key_frst).setOnTouchListener(keyListener);
    findViewById(R.id.key_scnd).setOnTouchListener(keyListener);
    findViewById(R.id.key_thrd).setOnTouchListener(keyListener);
    findViewById(R.id.key_frth).setOnTouchListener(keyListener);

}

活动配置,在此尝试中,有一个ConstraintLayout包含一个包含四个按钮的CustomLinearLayout(请参见下面的代码)。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:clickable="false"
    android:focusable="auto"
    android:focusableInTouchMode="false"
    tools:context=".KeyboardActivity">

    <com.unimi.lim.hmi.ui.CustomLinearLayour
        android:id="@+id/keyboard_vlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="100dp"
        android:clickable="false"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:id="@+id/key_frth"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="@string/key_frth" />

        <Button
            android:id="@+id/key_thrd"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="@string/key_thrd" />

        <Button
            android:id="@+id/key_scnd"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="@string/key_scnd" />

        <Button
            android:id="@+id/key_frst"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="@string/key_frst"
            tools:visibility="visible" />
    </com.unimi.lim.hmi.ui.CustomLinearLayour>

</androidx.constraintlayout.widget.ConstraintLayout>

CustomLinearLayout扩展了LinearLayout,在唯一的覆盖方法之下:我试图始终将触摸事件委托给布局子级]]

    @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Always delegate events to child
    return false;
}

谢谢,丹妮尔

我有一个由线性布局形成的活动,其中四个按钮作为子元素;布局覆盖整个屏幕尺寸,按钮仅覆盖一半的屏幕尺寸(请参见此处)。每个按钮都有一个触摸监听器...

android android-layout touch-event
1个回答
0
投票

onInterceptTouchEvent()的目的是获取触摸事件,而不是在返回值为false时将其发送给子级。默认返回值为false,因此自定义LinearLayout

© www.soinside.com 2019 - 2024. All rights reserved.