获取 Android 上触摸事件的坐标
我是 Android 新手,我已经完成了 hello world 教程,并且对正在发生的事情有了基本的了解。我对 T-Mobile Pulse 的触摸屏特别感兴趣,所以为了让我开始,我希望能够在屏幕上写下 tocuh 事件的坐标,假设用户触摸了坐标 5 ,2 - 屏幕上的文本视图将显示该内容。
目前我有一个简单的程序,只加载一个 xml 文件,其中包含我打算写入坐标的文本视图。
提前谢谢你,我在 Google 上寻求帮助并搜索了 stackoverflow,但我发现的所有内容都超出了我的想象或者不适合这个。干杯。
I'm new to Android, I've followed the hello world tutorial through and have a basic idea of what's going on. I'm particularly interested in the touch screen of my T-Mobile Pulse so just to get me started I want to be able to write the co-ordinates of a tocuh event on the screen, so say the user touched the co-ordinate 5,2 - a textview on the screen would display that.
At present I have a simple program that just loads an xml file which contains the textview I intend to write the co-ordinates in.
Thank you in advance, I did Google for help and searched stackoverflow but everything I found either went way over my head or wasn't suitable for this. Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用此功能: http: //developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)
您可能会大致这样将其放入 onCreate 方法中(这次经过测试):
Activity onCreate代码
布局代码
编辑:我尝试了我的代码,确实有一些错误。不管怎样,通过我在这里给你的布局,它可以在我的模拟器上运行。您能否提供更多代码/上下文,以便我看看出了什么问题?
You can use this function : http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)
You will probably put it in your onCreate method roughly this way (tested this time) :
Activity onCreate code
layout code
Edit: I tried my code and indeed there were a few errors. Anyway, with the layout I give you here it works on my emulator. Can you provide maybe more code/context so I can see what's wrong?
听起来您想从整个布局区域获取此特定测试的触摸事件。尝试将触摸侦听器附加到父视图而不是单独的目标视图。
这不是一种很常见的方法。在大多数情况下,您需要侦听特定子视图中的触摸事件,并且如果布局中还有其他处理触摸事件的视图(例如按钮),它们将优先于父视图。请参阅 http://developer.android.com/guide/topics/ui /ui-events.html 了解有关处理 UI 事件的更多信息。
布局(touch_viewer.xml):
在您的活动中:
It sounds like you want to get touch events from the whole area of your layout for this particular test. Try attaching the touch listener to your parent view rather than a separate target view.
This isn't a very common approach. In most cases you will want to listen for touch events in a specific child view and if you have other views in your layout that handle touch events (such as a button) they'll take priority over a parent view. See http://developer.android.com/guide/topics/ui/ui-events.html for more info about handling UI events.
Layout (touch_viewer.xml):
And in your activity:
以下是在 jetboy 示例 Android 应用程序上使用触摸屏进行的工作。
乔的代码进行了一项调整,使其在背景中闪闪发光。
我添加的唯一一行是
谢谢乔(上图)。
布局代码
The following works using a touch screen on the jetboy sample android app.
Joes code worked with one tweak to make it shine the background thru.
The only line I added was
Thanks Joe (above).
layout code
没有错误的更正版本:
The corrected version without the errors: