如何检查如果屏幕没有被点击Android中? [英] How to check if the screen hasn't been clicked in Android?

查看:542
本文介绍了如何检查如果屏幕没有被点击Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,如果你想检查是否有人上单击的东西,你会怎么做。

I know that if you want to check if someone clicked on something, you would do

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

但我想检查屏幕没有被点击的。
什么是code我会为此产生?

But I want to check if the screen hasn't been clicked at all. What is the code I would produce in order to do this?

此外,作为一个侧面说明,那会是一个不错的主意,有所有的类Android应用扩展查看类?

Also, as a side note, would it be a good idea to have all the classes in an Android app extend the View classes?

推荐答案

创建一个布尔变量名为 istouched 和使用 onTouchLisener 在你最上面的查看实际上它是一个窗口这就是你的查看是,快速出样

Create a boolean variable called istouched and use onTouchLisener on your top most View actually its a Window that's where your Views are, quick sample

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;
        }
    });

所以这里的逻辑是,如果 istouched <击>您的设备没有告诉她的妈妈,这意味着它没有被感动。

so the logic here is if istouched is false your device is not telling her mom, which means it has not being touched..

希望它能帮助

这篇关于如何检查如果屏幕没有被点击Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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