如何检查 Android 中屏幕是否未被点击?

How to check if the screen hasn't been clicked in Android?

我知道如果你想检查是否有人点击了某物,你会做

if(event.getAction() == MotionEvent.ACTION_DOWN)

但是我想检查屏幕是否根本没有被点击过。 我将为此生成什么代码?

此外,作为旁注,让 Android 应用程序中的所有 类 扩展 View 类 是否是个好主意?

一种方法是将 android:onClick="method" 添加到布局 xml 中的根布局。

在该方法中,您可以将 boolean 变量更改为 true (screenClicked = true)。

创建一个名为 istouchedboolean 变量并在最上面使用 onTouchLisener View 实际上它是一个 Window 那就是你的 Views 是,快速示例

getWindow().getDecorView().setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP){
                // i have been touched
                istouched = true;
                Toast.makeText(getBaseContext(), "you touched me?!! - i will tell my mom", Toast.LENGTH_SHORT).show();
                return true;
            }
            if(event.getAction() == MotionEvent.ACTION_DOWN){
                // you can implement this
                Toast.makeText(getBaseContext(), "shhh; i am touching", Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }
    });

所以这里的逻辑是如果 istouchedfalse 你的设备没有告诉她妈妈,这意味着它没有被触摸..

希望对您有所帮助