Activity 上的 OnTouchListener [英] OnTouchListener on Activity

查看:32
本文介绍了Activity 上的 OnTouchListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在一个按钮上有一个 OnTouchListener,它工作正常.代码如下:

Currently I have an OnTouchListener on a button, and it works properly. The code is as follows:

    Button button1 =(Button) findViewById(R.id.btOk1);
    button1.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            //do some stuff here
            return false;
        }
    });

我希望在我的整个 Activity 上有一个 OnTouchListener,这样每当用户点击我的 Activity 的任何区域时我都会做一些事情.我的活动的根布局是一个 TableLayout.我该怎么做?

I wish to have an OnTouchListener on my entire Activity instead, so that I do some stuff whenever the user taps on any area of my Activity. My activity's root layout is a TableLayout. How do I do this?

谢谢!

我设法通过命名我的根布局并设置一个 onTouchListener 来使用,如下所示:

I managed to do it by naming my root layout and setting an onTouchListener to use as follows:

    View view = findViewById(R.id.mylayout);
    view.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            Log.d(TAG, "onTouch");
            if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
                Log.d(TAG, "Obscured");
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    new AlertDialog.Builder(MyActivity.this)
                        .setTitle("Alert!")
                        .setMessage("Overlay detected!")
                        .setNeutralButton("Dismiss", null)
                        .show();
                }
                // Return true to prevent the button from processing the touch.
                return true;
            } else {
                Log.d(TAG, "Not obscured");
            }
            return false;
        }
    });

现在,每当我点击布局上的几乎任何地方时,都会收到触摸事件.我还可以通过将 MotionEvent 的标志与 FLAG_WINDOW_IS_OBSCURED 进行比较来检测将触摸事件传递给我的 Activity 的叠加层,但前提是触摸的区域实际上被遮挡了.此外,onTouchListener 似乎不会在触摸 Buttons 和 EditText 等 UI 元素时调用,而只会在空白处调用.

I'm now getting touch events whenever I click almost anywhere on the layout. I'm also able to detect overlays that pass touch events to my Activity by comparing the MotionEvent's flag with FLAG_WINDOW_IS_OBSCURED, but only if the area that was touched is actually obscured. Also, the onTouchListener doesn't seem to be called on touches on UI elements such as Buttons and EditText, but only on blank space.

有没有办法检测我的 Activity 的任何部分是否被遮挡,有没有办法为整个 Activity 设置一个 onTouchListener(),而不是为每个 UI 元素设置单独的一个?

Is there any way to detect if any part of my Activity is obscured, and is there any way of having a single onTouchListener() for the entire Activity, rather than having separate ones for each UI element?

推荐答案

将 OnTouchListener 应用到您的活动的根布局.这将确保问题.

Apply OnTouchListener to the root layout of your activty. that will sure the problem.

这篇关于Activity 上的 OnTouchListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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