当有人在 Android 应用程序中单击屏幕上的任意位置时,如何触发事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10126268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to fire an event when someone clicks anywhere on the screen in an android app?
提问by gurehbgui
how can I catch the event when click occurs somewhere on the app screen?
It doesn't matter if there is a button or something else. I just need to apply an onClick()
event listener on the whole application screen.
How to do this?
当应用程序屏幕上的某处发生点击时,如何捕捉事件?是否有按钮或其他东西都没有关系。我只需要onClick()
在整个应用程序屏幕上应用一个事件监听器。
这该怎么做?
采纳答案by Samir Mangroliya
setonclicklistner
for main layout of your layout file....
setonclicklistner
用于布局文件的主要布局....
Like....main.xml
喜欢....main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainlayout">
<!- Your other view->
</Relativelayout>
and set click listener for mainlayout...
并为 mainlayout 设置单击侦听器...
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.mainlayout);
rlayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
回答by Yevgeny Simkin
I believe the easiest way is to override onUserInteractionin your Activity
我相信最简单的方法是覆盖您的onUserInteractionActivity
Just be aware that it won't fire when users focus on/off EditText
s and Spinners
(probably other Widgets too). But it will fire every time the user touches the screen or presses a Button.
请注意,当用户专注于/关闭EditText
s 和Spinners
(也可能是其他小部件)时,它不会触发。但是每次用户触摸屏幕或按下按钮时它都会触发。
But this makes it unnecessary to build listeners into your layouts or write additional methods to handle those listeners, so I believe it's the most trouble-free way to get what you want.
但这使得在你的布局中构建监听器或编写额外的方法来处理这些监听器变得不必要,所以我相信这是获得你想要的最无故障的方式。
回答by Andrey Busik
You should just override Activity
's dispatchTouchEvent(ev: MotionEvent)
method
你应该只覆盖Activity
的dispatchTouchEvent(ev: MotionEvent)
方法
Look at this example in Kotlin. This approach will not affect any onClickListentersin your app
看看 Kotlin 中的这个例子。这种方法不会影响您的应用程序中的任何 onClickListenters
/**
* On each touch event:
* Check is [snackbar] present and displayed
* and dismiss it if user touched anywhere outside it's bounds
*/
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
// dismiss shown snackbar if user tapped anywhere outside snackbar
snackbar?.takeIf { it.isShown }?.run {
val touchPoint = Point(Math.round(ev.rawX), Math.round(ev.rawY))
if (!isPointInsideViewBounds(view, touchPoint)) {
dismiss()
snackbar = null // set snackbar to null to prevent this block being executed twice
}
}
// call super
return super.dispatchTouchEvent(ev)
}
Or check out the full gist
或者查看完整的要点
回答by Antzi
You can also directly set a callback from the onClick
xml attribute of the root layout:
也可以直接从onClick
根布局的xml 属性设置回调:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick"myMethod">
<!- Your other view->
</Relativelayout>
And in your activity:
在您的活动中:
public void myMethod(View pView) {
//XXX do something
}
Less bloated code this way :)
这样可以减少臃肿的代码:)
回答by Manitoba
Give your main layout an id, then
给你的主布局一个 id,然后
LinearLayout root = (LinearLayout) findViewById(R.id.main_layout);
root.setOnClickListener(new OnClickListener() {
public void onClick()
{
}
});
回答by DGomez
Maybe you can add a listener to the Layout instance, getting the layout with
也许您可以向 Layout 实例添加一个侦听器,以获取布局
RelativeLayout l = (RelativeLayout)findViewById(R.id.mylayout);
l.setOnClickListener(click);
回答by Alex Burov
If you want to know, that the user clicked anywhere on the screen then the trick is to override OnUserUnteraction():
如果你想知道用户点击了屏幕上的任何地方,那么诀窍是覆盖 OnUserUnteraction():
"Called whenever a key, touch, or trackball event is dispatched to the activity.Implement this method if you wish to know that the user has interacted with the device in some way while your activity is running. This callback and {@link #onUserLeaveHint} are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notification...."
"每当向活动分派按键、触摸或轨迹球事件时调用。如果您想知道用户在活动运行时以某种方式与设备进行了交互,请实现此方法。此回调和 {@link #onUserLeaveHint旨在帮助 Activity 智能地管理状态栏通知;具体来说,用于帮助 Activity 确定取消通知的适当时间......”
public abstract class BaseActivity extends Activity {
...
@Override
public void onUserInteraction() {
Log.d(DEBUG_TAG,"Touch anywhere happened");
super.onUserInteraction();
}
I am using it to set up app screen block after some inactivity.
在一些不活动后,我正在使用它来设置应用程序屏幕块。