【问题标题】:Prevent Click-through of Android ImageVIew?防止Android ImageVIew的点击?
【发布时间】:2014-05-29 14:19:23
【问题描述】:

我在RelativeLayout 内有一个ImageView 叠加层,并希望防止任何点击通过ImageView 到达它后面的按钮等(因此实际上禁用了整个RelativeLayout)。

这是一种更简单的方法,然后迭代RelativeLayout视图并将它们设置为禁用,就像我目前使用此代码所做的那样:

RelativeLayout rlTest = (RelativeLayout ) findViewById(R.id.rlTest);
for (int i = 0; i < rlTest.getChildCount(); i++) {
       View view = rlTest.getChildAt(i);
       view.setEnabled(true);
}

【问题讨论】:

    标签: java android android-layout click


    【解决方案1】:

    您可以像这样使用数据绑定并使用点击:

    android:onClick="@{() -> true}"
    

    【讨论】:

      【解决方案2】:

      有一种更清洁的方法

      你可以使用:

      android:onClick="preventClicks"
      

      在 XML 和您的活动中

      public void preventClicks(View view) {}
      

      这适用于片段。 此 Activity 中的示例有多个片段相互重叠,只需在片段的背景中添加 XML 属性,它仍然会调用 Activity.preventClicks 并防止触摸它后面的片段

      【讨论】:

        【解决方案3】:

        只需添加这两个监听器:

            // Set an OnTouchListener to always return true for onTouch events so that a touch
            // sequence cannot pass through the item to the item below.
            view.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    v.onTouchEvent(event);
                    return true;
                }
            });
            // Set an OnHoverListener to always return true for onHover events so that focus cannot
            // pass through the item to the item below.
            view.setOnHoverListener(new OnHoverListener() {
                @Override
                public boolean onHover(View v, MotionEvent event) {
                    v.onHoverEvent(event);
                    return true;
                }
            });
        

        【讨论】:

          【解决方案4】:

          以下解决方案适用于一般情况:

          _container.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  // NOTE: This prevents the touches from propagating through the view and incorrectly invoking the button behind it
                  return true;
              }
          });
          

          它基本上通过将触摸事件标记为已处理来阻止任何触摸在视图中传播。 这适用于 UI 控件和布局容器(即:LinearLayout、FrameLayout 等)。

          将“可点击”设置为 false 的解决方案对我在代码或视图 XML 中的布局容器不起作用。

          【讨论】:

          • 不要这样做。因为我这样发现了ACTION_UP MotionEvent会错过的BUG。
          【解决方案5】:

          只需致电rlTest.setClickable(false)。这将防止点击传播给孩子

          【讨论】:

          • 感谢您的回答,子视图仍在接收点击/点击事件,我是否还需要删除“点击”的处理程序(TapListener)。
          • 啊,太棒了,谢谢,成功了。我想这比迭代所有视图占用的资源要少得多!
          • 但再次设置 onClickListener 使其可点击!
          【解决方案6】:

          你可以设置图片为

          android:clickable="true"
          

          【讨论】:

          • 不幸的是,在处理对讲模式时,这不是一个理想的解决方案。 Talkback 将宣布它为可点击的,尽管视图没有关联任何内容。
          • 这仅在下视图不是按钮时有效。否则按钮仍在接收点击事件。
          【解决方案7】:

          我假设您正在使用 onClickListeners。

          如何使用 onTouchListener 而不是 onClickListeners。通过这样做,您将可以控制触摸在层次结构中的深度,甚至可以看到。例如,如果您在相对布局(RL)和图像视图(IV)(包含在 RL 中)上有 toch 侦听器,并且您将 touchListeners 分配给两者。现在如果你从 IV 的触摸事件中return true,下层成员 RL 将不会收到触摸事件。但是,如果您从 IV 的触摸事件中返回 false,则下层成员 RL 将收到触摸事件。

          希望这会有所帮助!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-11-21
            • 2023-03-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多