Java 防止点击 Android ImageVIew?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23080148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent Click-through of Android ImageVIew?
提问by Apqu
I have an ImageView
overlay inside of a RelativeLayout
and want to prevent any clicks from going through the ImageView
to the Buttons etc that are behind it (thus in effect disabling the entire RelativeLayout
).
我ImageView
在 a 内部有一个叠加层,RelativeLayout
并希望防止任何点击通过ImageView
它后面的 Buttons 等(因此实际上禁用了整个RelativeLayout
)。
Is the a simpler way of doing this then iterating the RelativeLayout views and setting them to disabled as I currently am doing using this code:
这是一种更简单的方法,然后迭代 RelativeLayout 视图并将它们设置为禁用,因为我目前正在使用以下代码:
RelativeLayout rlTest = (RelativeLayout ) findViewById(R.id.rlTest);
for (int i = 0; i < rlTest.getChildCount(); i++) {
View view = rlTest.getChildAt(i);
view.setEnabled(true);
}
采纳答案by Blackbelt
Simply call rlTest.setClickable(false)
. This will prevent the click to be propagate to the children
只需调用rlTest.setClickable(false)
. 这将防止点击传播给孩子
回答by Parth Kapoor
I assume that you are using onClickListeners.
我假设您正在使用 onClickListeners。
How about using onTouchListener instead of onClickListeners. By doing this you will have a control over how deep down in your hierarchy the touch even can be visible. For example, if you have toch listeners on a relative-layout(RL) and a image-view(IV)(contained in RL), and you assign touchListeners to both. Now if you return truefrom IV's touch event, the lower down member RL will not receive the touch event. However if you return falsefrom from IV's touch event, the lower down member RL will receive the touch event.
如何使用 onTouchListener 而不是 onClickListeners。通过这样做,您将可以控制在您的层次结构中触摸甚至可以看到的深度。例如,如果您在相对布局(RL)和图像视图(IV)(包含在 RL 中)上有 toch 侦听器,并且您将 touchListeners 分配给两者。现在,如果您从 IV 的触摸事件返回 true,则下层成员 RL 将不会收到触摸事件。但是,如果您从 IV 的触摸事件返回 false,则下层成员 RL 将收到触摸事件。
Hope this helps!
希望这可以帮助!
回答by Kirill Kulakov
you can set the image to be
您可以将图像设置为
android:clickable="true"
回答by Marchy
The following solution works in the general case:
以下解决方案适用于一般情况:
_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;
}
});
It basically blocks any touches from propagating through the view by marking the touch event as handled. This works on both UI controls and layout containers (ie: LinearLayout, FrameLayout etc.).
它基本上通过将触摸事件标记为已处理来阻止任何触摸在视图中传播。这适用于 UI 控件和布局容器(即:LinearLayout、FrameLayout 等)。
The solution to set "clickable" as false did not work for me for layout containers either in code or in the view XML.
对于代码或视图 XML 中的布局容器,将“可点击”设置为 false 的解决方案对我不起作用。
回答by Markos Evlogimenos
There is a much cleaner way
有一个更清洁的方法
You can use:
您可以使用:
android:onClick="preventClicks"
in XML and in your Activity
在 XML 和您的活动中
public void preventClicks(View view) {}
This works with fragments. Example inside this Activity has multiple fragments overlapping one another, just by adding the XML attribute in the background of your fragment it will still call the Activity.preventClicks and will prevent touches on fragments behind it
这适用于片段。此 Activity 中的示例具有多个彼此重叠的片段,只需在片段的背景中添加 XML 属性,它仍会调用 Activity.preventClicks 并防止对其后面的片段进行触摸
回答by Andrei Lupsa
Just add these two listeners:
只需添加这两个侦听器:
// 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;
}
});