Apq*_*pqu 18 java android click android-layout
我有一个ImageView叠加在一个内部,RelativeLayout并希望防止任何点击通过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);
}
Kir*_*kov 36
你可以设置图像
android:clickable="true"
Mar*_*nos 11
有一个更清洁的方式
您可以使用:
android:onClick="preventClicks"
在XML和您的Activity中
public void preventClicks(View view) {}
这适用于片段.这个Activity中的示例有多个片段相互重叠,只需在片段的后台添加XML属性,它仍然会调用Activity.preventClicks,并防止触及它背后的片段
以下解决方案适用于一般情况:
_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 等)。
对于代码或视图 XML 中的布局容器,将“clickable”设置为 false 的解决方案对我来说不起作用。