想给整个屏幕设置一个点击事件 当在屏幕上点击的时候 产生一个Onclick事件 但是我给一个
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rela);
relativeLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
但是这样貌似不行啊 在整个屏幕上设置Touch事件的监听用这个方法可以实现 但是单机事件好像不行啊
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
模拟出来的,模拟的过程在View
的touch
处理的几个方法中完成。触摸事件会由父View
传递给子View
,再由子View
冒泡到父View
,这个过程中,子View
是可能吃掉事件的,也就是说,触摸事件无法冒泡回父View
,既然收不到事件,父View
也就无法完成点击事件的模拟。解决方法是继承重写
View
的touch
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.