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

半城伤御伤魂 提交于 2019-12-11 03:19:23

问题


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

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

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?

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


回答1:


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

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

hope it helps




回答2:


One way is to add android:onClick="method" to the root layout in your layout xml.

in the method you can change a boolean variable to true (screenClicked = true).



来源:https://stackoverflow.com/questions/29836333/how-to-check-if-the-screen-hasnt-been-clicked-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!