简体   繁体   English

如何在视图上禁用触摸事件?

[英]How to disable touch events on a View?

My (OpenGL ES 2.0) app has a splashscreen which is basically an Android View displayed over my GLSurfaceView . 我的(OpenGL ES 2.0)应用程序具有启动屏幕,基本上是在我的GLSurfaceView上显示的Android 视图

I do all my initial setup on an AsyncTask and then (out of necessity), load my bitmaps/textures on the GLRendering thread in onSurfaceCreated. 我在AsyncTask上完成所有初始设置,然后(出于必要)在onSurfaceCreated中的GLRendering线程上加载我的位图/纹理。 Once everything has loaded, I dismiss my splashscreen. 一切加载完毕后,我关闭了启动画面。

Everything works as expected apart from one thing. 除了一件事情,一切都按预期进行。 The splashscreen still accepts input and if the screen is touched just after the app is launched, I (obviously) get an null pointer exception. 初始屏幕仍接受输入,如果在应用启动后立即触摸屏幕,则我(显然)会得到一个空指针异常。

I simply create my view like so: 我只是这样创建视图:

splashscreen = new SplashScreen(getApplication(), width, height);  //Where SplashScreen is a custom class that extends View

I then add it to my layout like so: 然后,将其添加到布局中,如下所示:

layout.add(myView);        //My GLSurfaceView
layout.add(splashscreen);  //The splashscreen View
setContectView(layout);

Once everything has loaded, I simply remove the (splashscreen) View to reveal my GLSurfaceView underneath: 加载完所有内容后,我只需删除(启动画面)视图以在下面显示我的GLSurfaceView:

layout.removeView(splashscreen);
splashscreen=null;  //Null it

currently, in my onTouch method, to get around the issue, I have put a check in place like so: 当前,在我的onTouch方法中,为了解决该问题,我已经进行了如下检查:

public boolean onTouchEvent(MotionEvent event) {

    if(splashscreen==null){
    //Do touch related stuff here
    }

    return true;

}

This does work, however, I'm sure it's not the cleanest way to achieve what I'm after. 这确实有效,但是,我敢肯定这不是实现我所追求的目标的最干净的方法。

I've tried this: 我已经试过了:

splashscreensetClickable(false);

and

layout.setClickable(false);

as well as setEnabled(false) 以及setEnabled(false)

Nothing seems to work, but I'm sure there must be a way of doing this....... 似乎没有任何效果,但是我敢肯定必须有一种方法来做.......

Generally, you will want the splash screen to prevent the user from doing anything while it is present. 通常,您希望启动屏幕阻止用户在屏幕出现时进行任何操作。 You can actually do this, because views in android, as in other UI frameworks, have a hierarchy in which touch events cascade. 实际上,您可以执行此操作,因为与其他UI框架一样,android中的视图具有触摸事件级联的层次结构。

Even though you don't want the splash screen to be interactive, you can still steal all of the touch events that occur on it, and just do nothing with them. 即使您不希望初始屏幕是交互式的,您仍然可以窃取其上发生的所有触摸事件,而对它们什么也不做。 The answer itself is to override onTouchEvent in your SplashScreen class with: 答案本身就是使用以下方法重写SplashScreen类中的onTouchEvent

@Override
public boolean onTouchEvent(MotionEvent event) {
    return true; // Splash screen is consuming all touch events on it.
}

Returning true in this basically says that nothing under this view should receive a touch event. 在此返回true基本上表示该视图下的任何内容都不应该接收触摸事件。 As long as it is visible and over top of the surface view, your surface view will not get any touch events. 只要可见并且在表面视图上方,您的表面视图就不会发生任何触摸事件。 See here for details. 有关详细信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM