Android:如何防止任何触摸事件从视图传递到视图下方? [英] Android: How to prevent any touch events from being passed from a view to the one underneath it?

查看:106
本文介绍了Android:如何防止任何触摸事件从视图传递到视图下方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专门使用下面的代码,有没有办法对其进行修改,以使这个新创建的视图下的活动不接收任何手势?

Specifically using the code below, is there a way to modify it so that the activity under this newly created view does not receive any gestures?

View v1 = new View(this);    
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
 1000,
 50,
 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
 PixelFormat.OPAQUE);

params.gravity = Gravity.BOTTOM;
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
wm.addView(v1, params);

推荐答案

向顶部位置的视图添加 onTouchEvent 方法,然后返回true.True会告诉事件冒泡该事件已被消耗,因此可以防止事件冒泡至其他视图.

Add an onTouchEvent method to the view with top position then return true. True will tell the event bubbling that the event was consumed therefore prevent event from bubbling to other views.

protected boolean onTouchEvent (MotionEvent me) {
    return true;
}

对于 v1 ,您需要导入:

import android.view.View.OnTouchListener;

然后设置onTouchListener:

Then set the onTouchListener:

v1.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

这篇关于Android:如何防止任何触摸事件从视图传递到视图下方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆