如何检测屏幕上的点击?
更具体地说,我应该在哪里附加 OnGestureListener
以便
我可以在屏幕上的任何位置检测到 onSingleTapUp
,
即使 ImageView
占据了一半屏幕。
现在我在具有 ImageView 的 Activity 上有了监听器。
但只有当我在 ImageView 外部单击时,侦听器才会触发。
我阅读并尝试理解这一点,但无法正确理解。
这段代码在活动中。
public boolean onSingleTapUp(MotionEvent e) {
//addtext.setText("-" + "SINGLE TAP UP" + "-");
//Log.d(TAG, "- + SINGLE TAP UP + - ***********************************************************************");
int btnsize = buttonSave.getHeight();
int viewWidth = display.getWidth();
int viewHeight = display.getHeight();
// RIGHT SIDE SCREEN
if(e.getX()> (viewWidth*0.7)){
Log.d(TAG, "RIGHT SIDE");
if(e.getY()> viewHeight*0.7){
Log.d(TAG, "right down on screen");
}else if(e.getY()> (viewHeight*0.45)){
Log.d(TAG, "right middle on screen ");
}
}
// LEFT SIDE SCREEN
if(e.getX()< (viewWidth*0.3)){
Log.d(TAG, "LEFT SIDE");
if(e.getY()> viewHeight*0.7){
Log.d(TAG, "Left middle on screen ");
}else if(e.getY()> (viewHeight*0.45)){
Log.d(TAG, "Left down on screen ");
}
}
return true;
}
More specific where do I attach OnGestureListener
so that
I can detect onSingleTapUp
everywhere on the screen,
even if an ImageView
take up half the screen.
Now I have the Listener on the Activity that has an ImageView.
But the Listener only fire when I click outside the ImageView.
I read and try to understand this but cannot get it right.
this code is in the Activity.
public boolean onSingleTapUp(MotionEvent e) {
//addtext.setText("-" + "SINGLE TAP UP" + "-");
//Log.d(TAG, "- + SINGLE TAP UP + - ***********************************************************************");
int btnsize = buttonSave.getHeight();
int viewWidth = display.getWidth();
int viewHeight = display.getHeight();
// RIGHT SIDE SCREEN
if(e.getX()> (viewWidth*0.7)){
Log.d(TAG, "RIGHT SIDE");
if(e.getY()> viewHeight*0.7){
Log.d(TAG, "right down on screen");
}else if(e.getY()> (viewHeight*0.45)){
Log.d(TAG, "right middle on screen ");
}
}
// LEFT SIDE SCREEN
if(e.getX()< (viewWidth*0.3)){
Log.d(TAG, "LEFT SIDE");
if(e.getY()> viewHeight*0.7){
Log.d(TAG, "Left middle on screen ");
}else if(e.getY()> (viewHeight*0.45)){
Log.d(TAG, "Left down on screen ");
}
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检测屏幕点击
我正在为那些只需要一种简单的方法来检测屏幕上的点击的人回答这个问题:
将
android:onClick
值添加到您的基本/根布局(LinearLayout、RelativeLayout) , ETC。)。您可以随意命名它,我将其命名为screenTapped
作为示例:使用您为
指定的相同名称将此方法添加到您的
:Activity
>onClick现在,点击屏幕将调用该方法上面。
Detect Screen Tap
I'm answering this for those who just need a simple way to detect a tap on the screen:
Add an
android:onClick
value to your base/root layout (LinearLayout, RelativeLayout, etc.). You can call it anything you want, I'm naming itscreenTapped
as an example:Add this method to your
Activity
using the same name you specified foronClick
:Now, tapping on the screen will call the method above.
我正在回答我自己的问题。感谢上面的@Jason。我已将
onTouchEvent
注册到所有视图。效果很好。
I'm answering my own question. Thanks to the above @Jason. I have registered
onTouchEvent
to all views.It works great.