java - 关于android的整个屏幕的点击事件
PHP中文网
PHP中文网 2017-04-17 17:48:49
0
6
352

想给整个屏幕设置一个点击事件 当在屏幕上点击的时候 产生一个Onclick事件 但是我给一个

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rela);
relativeLayout.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
            }
        });
        

但是这样貌似不行啊 在整个屏幕上设置Touch事件的监听用这个方法可以实现 但是单机事件好像不行啊

PHP中文网
PHP中文网

认证0级讲师

reply all(6)
Peter_Zhu

RelativeLayout is essentially a view, so it’s no problem to set a listener for it. You didn’t set it to match_parent?

伊谢尔伦

Points to note are:
1. Whether the RelativeLayout you set fills the entire screen can be determined by setting the background color.
2. Is there any event coverage? For example, if there is a button on RelativeLayout and the button you click, the button event will conflict with your RelativeLayout
event. In this case, the button will cover the event of RelativeLayout# 🎜🎜#

阿神

There are no 点击事件的,点击事件是有View模拟出来的,模拟的过程在Viewtouch处理的几个方法中完成。触摸事件会由父View传递给子View,再由子View冒泡到父View,这个过程中,子View是可能吃掉事件的,也就是说,触摸事件无法冒泡回父View,既然收不到事件,父View也就无法完成点击事件的模拟。
解决方法是继承重写Viewtouch processing methods in the underlying touch events, in which you can complete the simulation of click events by yourself.

洪涛

relativeLayout.setClickable(true);

巴扎黑

FrameLayout rootview=(FrameLayout)findViewById(android.R.id.content);
rootview.setClickable(true);

左手右手慢动作

I suggest you take a look at the Android event distribution mechanism to understand why it doesn’t work.
If you must implement this function, set the OnClickListener of all views to the same object.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template