HarmonyOS 鸿蒙Next 自定义的ComponentContainer总是触发上层穿透的点击事件

HarmonyOS 鸿蒙Next 自定义的ComponentContainer总是触发上层穿透的点击事件 自定义的组件继承ComponentContainer,在使用时,使用布局StackLayout,然后使用该自定义组件,然后在上层添加一个button按钮。给组件和button都设置点击事件,点击button的时候,自定义组件的点击事件也会触发,请问是什么原因。急急急急。

3 回复

楼主你好,能不能提供下代码片段,方便更好定位

更多关于HarmonyOS 鸿蒙Next 自定义的ComponentContainer总是触发上层穿透的点击事件的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


布局文件:

ohos:height="match_content"
ohos:width="match_parent"
smallView="true"
smallViewBottom="200"
smallViewEnableEvent="true"
smallViewLeft="100"
smallViewRight="200"
smallViewTop="100"

$+id:btn_block
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:wight_round_corner_bg"
ohos:clickable="true"
ohos:focusable="focus_enable"
ohos:padding="5vp"
ohos:text="障碍区"
ohos:text_size="14fp"

按钮点击事件

```java
btn_block.setClickedListener(new ClickedListener() {
    @Override
    public void onClick(Component component) {
        btn_block_OK.setVisibility(VISIBLE);
        btn_block_clean.setVisibility(VISIBLE);
        btn_block.setVisibility(HIDE);
        CommonUtils.showTips(getContext(), "请在地图左下和右上选择两个点作为障碍区边界");
        isBlock = true;
    }
});

自定义View的点击事件

mMapView.addOnMapClickListener(new MapView.OnMapClickListener() {
    @Override
    public void onMapClick(Point point) { 
    }
});

自定义view:

public class MapView extends ComponentContainer implements Listener, Renderer, EstimateSizeListener, ArrangeListener, LongClickedListener, DoubleClickedListener, RotationEventListener, ScaledListener, TouchEventListener {
    private static final String TAG = "[MapView]";
    private static final long ONDRAWFRAME_TIMEOUT = 100L;
    private static final int REFRESH = 0;
    private static final int IDLE = 1;
    private static final int NATIVE_CONDITION_LOCK = 2;
    private static final int PAUSE_GL_SURFACE_VIEW = 3;
    private static final int RESUME_GL_SURFACE_VIEW = 4;
    private static final int RESIZE_WINDOW = 5;
    private static final int INITED = 6;
    private static boolean mEnableRecreateGLContext = true;
    private Context mContext;
    private MapView.CustomGlSurfaceProvider mGlSurfaceProvider;
    protected MineMap mMineMap;

    var5 = this.mMineMap.screen2World(var3);
    if (this.mOnMapClickListener != null) {
        this.mOnMapClickListener.onMapClick(var5);
    }

    public interface OnMapClickListener {
        void onMapClick(Point var1);
    }
}

在HarmonyOS鸿蒙Next系统中,如果你自定义的ComponentContainer总是触发上层穿透的点击事件,这通常与事件分发机制有关。可能的原因包括但不限于:

  1. 事件拦截未正确处理:检查你的ComponentContainer是否重写了相关的事件拦截方法(如onTouchEventinterceptTouchEvent),并确保在这些方法中正确判断并处理事件,防止其继续向上层传递。

  2. 子组件事件消费不足:确保容器内的子组件能够正确消费点击事件。如果子组件没有消费事件,事件可能会继续传递,导致上层组件响应。

  3. 属性设置不当:检查是否有属性设置导致事件穿透,例如某些特定的布局属性或焦点属性可能影响事件分发。

  4. 版本差异:不同版本的HarmonyOS可能在事件处理机制上存在细微差异,确保你的代码与当前系统版本兼容。

针对这些问题,你可以检查并调整事件拦截和处理的逻辑,确保事件在合适的组件中被消费。同时,验证子组件的事件处理逻辑,避免事件未被正确消费而继续传递。

如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html

回到顶部